How can I “try to do something and then detect if it fails” in bash?

前端 未结 3 1959
后悔当初
后悔当初 2020-12-19 14:21

In an answer to a previous question:

How can I use 'do I have root access?' as a conditional in bash?

The suggestion to \'try to do something and de

3条回答
  •  不知归路
    2020-12-19 15:06

    unless the script you are calling has an exit condition, there isn't much you can do. However look up "set" in the bash man page.

    set -e
    

    will cause a script to exit if a simple command in it fails. You can add it to the top of script.sh in your example to cause it to exit if it fails.

    also look at trap. I believe

    trap 'exit 2' ERR
    

    is similar

提交回复
热议问题