How to check the exit status using an if statement

后端 未结 7 1666
北荒
北荒 2020-11-29 16:27

I was wondering what would be the best way to check the exit status in an if statement in order to echo a specific output.

I\'m thinking of it being

         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 16:50

    For the record, if the script is run with set -e (or #!/bin/bash -e) and you therefore cannot check $? directly (since the script would terminate on any return code other than zero), but want to handle a specific code, @gboffis comment is great:

    /some/command || error_code=$?
    if [ "${error_code}" -eq 2 ]; then
       ...
    

提交回复
热议问题