【发布时间】:2014-03-23 23:02:03
【问题描述】:
我正在从machineA 运行我的下面的shell 脚本,它将文件从machineB 和machineC 复制到machineA。如果文件不在machineB 中,那么它应该在machineC 中。文件不会在machineB 和machineC 之间重复,它们将具有唯一的文件,所以假设我总共有100 files,所以40 files 可能在machineB 中,剩余的60 files 将是在machineC。
我需要复制machineA中PRIMARY目录中的一些文件和machineA中SECONDARY目录中的一些文件。
下面是我的 shell 脚本,它运行良好,但我想处理一些极端情况,例如 -
- 在某些情况下,假设如果文件在
machineB或machineC中不可用,那么我想以非零状态(不成功)退出 shell 脚本并返回文件错误在machineB和machineC中都没有的号码。
下面是我的shell脚本-
#!/bin/bash
readonly PRIMARY=/test01/primary
readonly SECONDARY=/test02/secondary
readonly FILERS_LOCATION=(machineB machineC)
readonly LOCATION=/bat/test/pe_t1_snapshot
PRIMARY_PARTITION=(1003 1012 1031) # this will have more file numbers
SECONDARY_PARTITION=(1005 1032 1067) # this will have more file numbers
dir1=/bat/test/pe_t1_snapshot/20140320
dir2=/bat/test/pe_t1_snapshot/20140320
if [ "$dir1" = "$dir2" ]
then
# delete first and then copy the files in primary directory
find "$PRIMARY" -mindepth 1 -delete
for el in "${PRIMARY_PARTITION[@]}"
do
scp user@${FILERS_LOCATION[0]}:$dir1/t1_weekly_1680_"$el"_200003_5.data $PRIMARY/. || scp user@${FILERS_LOCATION[1]}:$dir2/t1_weekly_1680_"$el"_200003_5.data $PRIMARY/.
done
# delete first and then copy the files in secondary directory
find "$SECONDARY" -mindepth 1 -delete
for sl in "${SECONDARY_PARTITION[@]}"
do
scp user@${FILERS_LOCATION[0]}:$dir1/t1_weekly_1680_"$sl"_200003_5.data $SECONDARY/. || scp user@${FILERS_LOCATION[1]}:$dir2/t1_weekly_1680_"$sl"_200003_5.data $SECONDARY/.
done
fi
当我从 Python subprocess 模块调用上述 shell 脚本时 -
proc = subprocess.Popen(shell_script, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, executable='/bin/bash')
(stdout, stderr) = proc.communicate()
if proc.returncode != 0:
# log an error with message which file numbers are missing in machineB and machine
else:
# log successfull status
在我上面的 shell 脚本中,我使用的是管道 || 运算符。如果文件不在machineB 中,则使用管道|| 运算符尝试machineC。
这可能吗?
【问题讨论】:
-
可以直接从 Python 运行
scp吗?
标签: python linux bash shell subprocess