如果两台机器上都缺少文件,如何以非零状态退出shell脚本? [英] How to exit out of the shell script with non zero status if the files are missing in both the machines?
问题描述
我正在从 machineA
运行下面的shell脚本,该脚本将文件从 machineB
和 machineC
复制到 machineA
.如果文件不在 machineB
中,则应该在 machineC
中.文件不会在 machineB
和 machineC
之间重复,它们将具有唯一的文件,所以说我总共有 100个文件
,因此可能是可能的 40个文件
位于 machineB
中,其余的 60个文件
将位于 machineC
中.
I am running my below shell script from machineA
which is copying the files from machineB
and machineC
into machineA
. If the files are not there in machineB
, then it should be there in machineC
. Files won't be duplicated across machineB
and machineC
, they will have unique files so let's say I have total 100 files
, so it might be possible 40 files
are in machineB
and remaining 60 files
will be in machineC
.
我需要在 machineA
的 PRIMARY
目录中复制一些文件,并在 machineA
的 SECONDARY
目录中复制一些文件
I need to copy some files in PRIMARY
directory in machineA
and some files in SECONDARY
directory in machineA
.
下面是我的shell脚本,可以正常工作,但我想处理一些极端情况,例如-
Below is my shell script which works fine but I would like to handle some corner cases such as -
- 在某些情况下,假设文件在
machineB
或machineC
中都不可用,那么我想退出状态为非零的shell脚本(是不成功的),并返回错误代码和machineB
和machineC
都不可用的文件号.
- In some cases suppose if the files are not available either in
machineB
ormachineC
, then I would like to exit out of the shell script with non zero status (which is unsuccessfull) and return the error with the file number that are not available inmachineB
andmachineC
both.
下面是我的shell脚本-
Below is my shell script -
#!/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子进程
模块-
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
中进行尝试.
In my above shell script I am using pipe ||
operator. If the files are not there in machineB
then try in machineC
using pipe ||
operator.
这可能吗?
推荐答案
如果需要获取文件名和现有代码,请尝试此操作,还需要使用其他现有代码(例如 exit)设置次要scp102
If you need get the file name and exist code, try this, you need set for secondary scp as well with different exist code, such as exit 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
这篇关于如果两台机器上都缺少文件,如何以非零状态退出shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!