有人能解释一下在bash这个try / catch语句的选择吗? [英] Could someone explain this try/catch alternative in bash?
本文介绍了有人能解释一下在bash这个try / catch语句的选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我才发现原来的bash不处理异常(没有的try / catch)。
对于我的剧本,我想知道,如果命令是成功还是失败。
So I found out that bash does not handle exceptions (there is no try/catch). For my script, I would like to know if a command was successful or not.
这是我的code的部分现在:
This is the part of my code right now:
command = "scp -p$port $user:$password@$host:$from $to"
$command 2>/dev/null
if (( $? == 0 )); then
echo 'command was successful'
else
echo 'damn, there was an error'
fi
我不明白的事情是:
The things I don't understand are:
- 第3行,为什么我必须把
2
后面的$命令
? - 第5行,它究竟是什么这个
$
?
- line 3, why do I have to put the
2
behind the$command
? - line 5, what exactly is it with this
$
?
推荐答案
$?
表示最后执行的命令的返回code。
$?
means the return code of the last executed command.
2 - ;
表示重定向标准错误
(标准错误流)输出到的/ dev /空
。
2>
means redirecting the stderr
(standard error stream) output to /dev/null
.
这篇关于有人能解释一下在bash这个try / catch语句的选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文