实际收益code为SCP [英] Actual return code for SCP
问题描述
我写一个bash脚本,通过文件名的例子不胜枚举,并尝试使用 SCP
每个文件从两个服务器复制到本地文件夹。然后,该脚本本地文件相互比较。然而有时,该文件将不会在一台服务器或另一个或两个上存在。
I am writing a bash script that goes through a list of filenames and attempts to copy each file using scp
from two servers into a local folder. The script then compares the local files to each other. Sometimes however, the file will not exist on one server or the other or both.
起初,我就是用这个code:
At first, I was using this code:
scp $user@$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
error=$(</tmp/Error) # error catching
if [[ -n "$error" ]]; then echo -e "$file not found on $host"; fi
但我发现,一些(企业)服务器输出(法律术语)消息(到stderr我猜)的每一个用户通过 SCP
连接时或 SSH
。于是我开始寻找到利用<一个href=\"http://stackoverflow.com/questions/921210/can-i-know-abt-the-exit-status-of-the-scp-command-in-unix\">exit codeS 。
But I found that some (corporate) servers output a (legalese) message (to stderr I guess) every time a user connects via scp
or ssh
. So I started looking into utilizing exit codes.
我可以简单地用
scp $user@$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
if [[ $? -ne 0 ]]; then echo -e "$file not found on $host"; fi
但由于退出code表示文件不存在应该是6 ,我宁愿有一个更precise
but since the exit code for "file does not exist" is supposed to be 6, I would rather have a more precise
scp $user@$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
if [[ $? -eq 6 ]]; then echo -e "$file not found on $host"; fi
问题是,我似乎得到1退出code不管是什么地方出了错。这个问题类似于这一个,但这个问题的答案并不能帮助我击。
The problem is that I seem to be getting an exit code of 1 no matter what went wrong. This question is similar to this one, but that answer does not help me in Bash.
我正在考虑另一种方案是
Another solution I am considering is
scp $user@$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
error=$(</tmp/Error) # error catching
if [[ ${error: -25} = "No such file or directory" ]]; then echo -e "$file not found on $host"; fi
但我关注的是不同版本的 SCP
的可能对相同的错误不同的错误消息。
But I am concerned that different versions of scp
could have different error messages for the same error.
有没有办法让 SCP
的实际退出code在Bash脚本?
Is there a way to get the actual exit code of scp
in a Bash script?
推荐答案
每评论( @gniourf_gniourf , @shelter , @Wintermute ),我决定简单地切换工具的rsync
。值得庆幸的语法不需要在所有被改变。
Per the comments (@gniourf_gniourf, @shelter, @Wintermute) I decided to simply switch tools to rsync
. Thankfully the syntax doesn't need to be changed at all.
23
是错误code时,不存在这样的文件在这里我得到的是code我结束了
23
was the error code I was getting when files didn't exist so here is the code I ended up with
rsync -q $user@$host:/etc/$file ./$host/conf/ 2>/tmp/Error 1>/dev/null
if [[ $? -eq 23 ]]; then echo -e "$file not found on $host"; continue; fi
这篇关于实际收益code为SCP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!