Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果两台机器都缺少文件,如何退出非零状态的shell脚本?_Python_Linux_Bash_Shell_Subprocess - Fatal编程技术网

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
    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子流程
模块调用上述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运行退出codecould
scp