proper way to detect shell exit code when errexit option set

后端 未结 14 1851
甜味超标
甜味超标 2021-01-30 12:52

I prefer to write solid shell code, so the errexit & nounset is alway set.

The following code will stop at bad_command line

#!/bin/b         


        
14条回答
  •  半阙折子戏
    2021-01-30 13:29

    what if you want to know exit status of bad_command?

    I think the simplest way is to disable errexit:

    #!/bin/sh
    set -o errexit
    
    some_code_here
    
    set +o errexit
    bad_command
    status=$?
    set -o errexit
    process $status
    

提交回复
热议问题