bash 检查 scp 命令的状态码,如果失败,则在另一台机器上调用 scp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20627207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:01:02  来源:igfitidea点击:

Check the status code of a scp command and if it is failed, then call scp on another machine

linuxbashshellunixscp

提问by AKIWEB

Below is my snippet of shell script in which I am executing scp command to copy the files from machineBto machineA.

下面是我在我执行scp命令将文件从shell脚本的片断machineBmachineA

for element in ${x[$key]}; do   # no quotes here
    printf "%s\t%s\n" "$key" "$element"
    if [ $key -eq 0 ]
    then
        scp david@machineB:/data/be_t1_snapshot/20131215/t1_$element_5.data /data01/primary/.
    fi    
done

I have a very simple question which is mentioned below -

我有一个非常简单的问题,如下所述-

If the above scp command in my shell script gives me this error for whatever reason - No such file or directorythen I need to try doing scpfrom machineCand for that scp command will be like this, only machine will be different and everything else will be same -

如果我的 shell 脚本中的上述 scp 命令无论出于何种原因给我这个错误 - No such file or directory那么我需要尝试scpmachineCscp 命令开始,因为该 scp 命令将是这样的,只有机器会有所不同,其他一切都会相同 -

scp david@machineC:/data/be_t1_snapshot/20131215/t1_$element_5.data /data01/primary/.

So my question is how to check the output of the above scp command in my shell script and then decide whether I need to call scp command from machineC or not? Is there any status kind of thing which I can use to check and if it got failed for whatever reason, then I can call scp command on machineC?

所以我的问题是如何在我的 shell 脚本中检查上述 scp 命令的输出,然后决定是否需要从 machineC 调用 scp 命令?是否有任何状态类型的东西可以用来检查,如果它因某种原因失败,那么我可以在 machineC 上调用 scp 命令?

Is this possible to do in shell script?

这可以在shell脚本中完成吗?

采纳答案by janos

Here you go:

干得好:

for element in ${x[$key]}; do   # no quotes here
    printf "%s\t%s\n" "$key" "$element"
    if [ $key -eq 0 ]
    then
        scp david@machineB:/data/be_t1_snapshot/20131215/t1_$element_5.data /data01/primary/. || scp david@machineB:/data/be_t1_snapshot/20131215/t1_$element_5.data /data01/primary/
    fi    
done

Well-behaving commands exit with "success" (exit code = 0) if the operation was successful, or otherwise with an exit code != 0. You can chain commands together like this:

如果操作成功,行为良好的命令将以“成功”(退出代码 = 0)退出,否则以退出代码 != 0 退出。您可以像这样将命令链接在一起:

cmd && echo successful || echo failed
cmd && keep going || do something else

The exit code is also stored in the $?variable, so this is equivalent:

退出代码也存储在$?变量中,所以这是等效的:

cmd; if $? = 0; then echo successful; else echo failed; fi

Not only this is possible, the status code of commands is extremely important in shell scripting. Consider these two examples:

不仅这是可能的,命令的状态代码在 shell 脚本中也非常重要。考虑这两个例子:

./configure && make && make install
./configure; make; make install

The first one will execute the chain of commands if all are successful. The second will execute all of them always, even if an earlier command failed.

如果全部成功,第一个将执行命令链。第二个将始终执行所有这些,即使之前的命令失败。

回答by ray

scp returns 0 only wen it succeeds. so you can write like this:

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

scp machineB:/path/toyourfile .
if [ $? -ne 0 ]
then
    scp machineC:/path/to/your/file .
fi

a shorter way is:

更短的方法是:

scp machineB:/path/toyourfile .
[ $? -eq 0 ] || scp machineC:/path/to/your/file .

or

或者

scp machineB:/path/toyourfile .
[ $? -ne 0 ] && scp machineC:/path/to/your/file .

personally I prefer the even shorter way, and the scp output is of no use in script:

我个人更喜欢更短的方式,并且 scp 输出在脚本中没有用:

scp -q machineB:/path/to/your/file . || scp -q machineC:/path/to/your/file .

and remember to use ${element}instead of $element

并记住使用${element}代替$element