Python 如果两台机器都缺少文件,如何退出非零状态的shell脚本?
我正在从Python 如果两台机器都缺少文件,如何退出非零状态的shell脚本?,python,linux,bash,shell,subprocess,Python,Linux,Bash,Shell,Subprocess,我正在从machineA运行我的shell脚本,它正在将文件从machineB和machineC复制到machineA。如果文件不在machineB中,那么它应该在machineC中。文件不会在machineB和machineC之间复制,它们将具有唯一的文件,因此假设我总共有100个文件,因此可能40个文件在machineB中,其余的60个文件将在machineC中 我需要复制machineA中PRIMARY目录中的一些文件,以及machineA中SECONDARY目录中的一些文件 下面是我的s
machineA
运行我的shell脚本,它正在将文件从machineB
和machineC
复制到machineA
。如果文件不在machineB
中,那么它应该在machineC
中。文件不会在machineB
和machineC
之间复制,它们将具有唯一的文件,因此假设我总共有100个文件
,因此可能40个文件
在machineB
中,其余的60个文件
将在machineC
中
我需要复制machineA
中PRIMARY
目录中的一些文件,以及machineA
中SECONDARY
目录中的一些文件
下面是我的shell脚本,它工作得很好,但我想处理一些特殊情况,例如-
- 在某些情况下,假设文件在
或machineB
中都不可用,那么我希望以非零状态(未成功)退出shell脚本,并返回文件号为的错误,该错误在machineC
和machineB
中都不可用machineC
#!/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子流程
模块调用上述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脚本中,我使用了pipe|
操作符。如果文件不在machineB
中,请使用管道|
操作符尝试machineC
可以这样做吗?如果需要获取文件名和存在代码,请尝试此操作,您需要为辅助scp设置不同的存在代码,例如退出102
# delete first and then copy the files in primary directory
find "$PRIMARY" -mindepth 1 -delete
for el in "${PRIMARY_PARTITION[@]}"
do
if `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/. ` ; then
:
else
echo " scp is not successful on file $dir1/t1_weekly_1680_${el}_200003_5.data"
exit 101
done
你可以用
退出$
或
退出$LINENO
使用非成功代码退出shell脚本。谢谢您的帮助。愚蠢的问题-这里101和102是退出代码?对的哪些文件号丢失了呢?我们可以在Python脚本的
stderr
中获得这些信息吗?请参阅我的更新。您可以使用命令echo$?
直接从Python运行退出codecouldscp
?