gpt4 book ai didi

linux - 如果两台机器都缺少文件,如何退出 shell 脚本?

转载 作者:太空狗 更新时间:2023-10-29 12:16:25 26 4
gpt4 key购买 nike

我正在 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

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

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

最佳答案

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

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

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

关于linux - 如果两台机器都缺少文件,如何退出 shell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23075605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com