在回答前一个问题:如何在bash中“尝试做某件事,然后检测它是否失败”?
How can I use 'do I have root access?' as a conditional in bash?
的建议,“尽量做一些事情,如果它不能检测”而不是“检查许可,再做事”
我发现充足的理由对这个如:
- Thorough use of 'if' statements or 'try/catch' blocks?
- What is the advantage of using try {} catch {} versus if {} else {}
但是,我发现很少有关于如何在bash中实现try/catch的清晰信息。我猜想,这是太容易了,除了使用函数或其他脚本什么我发现似乎相当complicated-:
我是比较新的,但来砸困惑在其他语言中没有类似于try
函数的简单功能。
具体而言,我想做到以下几点:
CMD=`./path/to/script.sh`
if [ <echo $CMD | grep error is true> ]; then
.. do this ..
else
.. do that ..
fi