【问题标题】:How to exit out of the shell script if the files are missng in both the machines?如果两台机器都缺少文件,如何退出 shell 脚本?
【发布时间】:2014-05-29 07:53:13
【问题描述】:

我在machineA 上运行我的shell 脚本,它将文件从machineBmachineC 复制到machineA

如果文件不在machineB 中,那么它肯定在machineC 中。所以我会先尝试从machineB 复制,如果machineB 中没有,我会去machineC 复制相同的文件。下面是我用来复制文件的 shell 脚本,PRIMARY_PARTITION 是一个包含所有文件编号的数组 -

for el in "${PRIMARY_PARTITION[@]}"
do
    scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineB:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/. || scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineC:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/.
done

现在的问题是 - 在某些情况下,两台机器上都不存在文件 - machineBmachineC 然后我想以非零状态和正确消息退出 shell 脚本缺少哪个文件 -

所以我这样做了,我在最后添加了另一个管道语句,但不知何故,该消息没有在屏幕上打印出来。而且我很确定那些试图复制的文件不在这两台机器的那些文件夹中 -

scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineB:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/. || scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineC:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/. || echo "File number $el missing on both the machines for primary partition." >&2; exit 1

所以它应该转到最后一个管道语句并在控制台上打印出来。对吧?

我在上一个管道语句中做错了什么吗?

【问题讨论】:

标签: linux bash shell unix scp


【解决方案1】:

根据您的解决方案,它应该可以工作

scp 仅在成功时返回 0。所以你可以这样写:

scp -q machineB:/path/to/your/file . || scp -q machineC:/path/to/your/file . || echo "Sorry no file found"

现在让您首先检查每个命令的重新运行代码,如下所示

scp -o ControlMaster=auto -o 'ControlPath=~/.ssh/control-%r@%h:%p' -o ControlPersist=900 david@machineB:/te/data/te_snapshot/20140411/t1_weekly_1680_"$el"_200003_5.data /data01/primary/.

if [[ $? -ne 0 ]]; then
      echo "No file found"
      exit 1
else
     echo "oh..file is here!"
fi

第二个命令也一样。现在在这两种情况下,如果您获得非零值,那么您的解决方案必须有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2016-06-23
    • 1970-01-01
    • 2018-08-10
    • 2013-09-07
    相关资源
    最近更新 更多