Bash脚本到Python

2024-03-29 13:29:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把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/

Tags: 命令脚本bashmodulesiflinuxpwdfiles
1条回答
网友
1楼 · 发布于 2024-03-29 13:29:15
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.

最好这样写:

^{pr2}$

相关问题 更多 >