How to check the exit status using an if statement

后端 未结 7 1614
北荒
北荒 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:55

    If you are writing a function, which is always preferred, you should propagate the error like this:

    function() {
        if some_command; then
            echo "Worked"
        else
            return "${?}"
    fi
    }
    

    This will propagate the error to the caller, so that he can do things like function && next as expected.

    0 讨论(0)
提交回复
热议问题