-3

我想将 bash 脚本转换为 Python。我无法理解 bash 的某些命令。这是 bash 脚本 -

L=`basename $PWD`
D=`dirname $PWD`
S=`basename $D`_$L

M=`ls -d /ccshare/linux/c_files/anand/*$S | head -n 1`

if [ -z $M ] ; then
    can not find module directory
    exit 1
fi

mkdir -p modules && cp `find $M -type f` modules/ && chmod -R u+rw modules/
4

1 回答 1

3
L=`basename $PWD`  # Get name of the current directory
D=`dirname $PWD`   # Get the path to the current directory, without its name.
S=`basename $D`_$L # Append the name of the current directory to the name of the directory below it.

M=`ls -d /ccshare/linux/c_files/anand/*$S | head -n 1` # A basically insane way to get the first file in that directory whose name ends with the $S name above.

if [ -z $M ] ; then
    can not find module directory # Broken; presumably meant to be printed to stderr.
    # (printf '%s\n' 'Cannot find module directory.' ?)
    exit 1 # Signal failure to any future commands.
fi

mkdir -p modules && # Make the modules directory.
cp `find $M -type f` modules/ && # A basically insane way to copy the files in "the module directory" to the directory named "modules"
chmod -R u+rw modules/ # Set permissions recursively, even though this directory will be flat.

最好这样写:

#!/bin/bash

# Get the name of the directory below this one.
dirBelow="${PWD%/*}"
dirBelow="${dirBelow##*/}"

# Make the modules directory.
mkdir -p modules
# Copy the files in the "module directory" into the directory named "modules".
find /ccshare/linux/c_files/anand/*"$dirBelow_${PWD##*/}" -type f -exec cp {} "$PWD/modules/" +
chmod u+rw modules/*
于 2013-05-30T17:03:58.390 回答