【问题标题】:How to exit out of the shell script with non zero status if the files are missing in both the machines?如果两台机器都缺少文件,如何以非零状态退出 shell 脚本?
【发布时间】:2014-03-23 23:02:03
【问题描述】:

我正在从machineA 运行我的下面的shell 脚本,它将文件从machineBmachineC 复制到machineA。如果文件不在machineB 中,那么它应该在machineC 中。文件不会在machineBmachineC 之间重复,它们将具有唯一的文件,所以假设我总共有100 files,所以40 files 可能在machineB 中,剩余的60 files 将是在machineC

我需要复制machineAPRIMARY目录中的一些文件和machineASECONDARY目录中的一些文件。

下面是我的 shell 脚本,它运行良好,但我想处理一些极端情况,例如 -

  • 在某些情况下,假设如果文件在machineBmachineC 中不可用,那么我想以非零状态(不成功)退出 shell 脚本并返回文件错误在machineBmachineC 中都没有的号码。

下面是我的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


【解决方案1】:

如果你需要获取文件名和存在码,试试这个,你需要为二级scp设置不同的存在码,例如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

【讨论】:

  • 感谢您的帮助。愚蠢的问题 - 这里 101 和 102 是退出代码?正确的?那么缺少哪些文件编号呢?我们可以在 Python 脚本中的 stderr 中获取该信息吗?
  • 查看我的更新。您可以使用命令echo $? 获取退出代码
【解决方案2】:

你可以使用

退出$?

退出 $LINENO

退出带有非成功代码的 shell 脚本。

【讨论】:

    猜你喜欢
    • 2014-05-29
    • 2022-06-21
    • 2015-04-30
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    相关资源
    最近更新 更多