Donnerstag, 8. März 2012

If condition with dependance on previous command ( $? )

Say youre writing a bash script and have an if condition in it that depends on whether or not the previous condition was "successful".

Example:

sleep 10; if (( $? )); then echo "sleep interrupted!";fi

now if you cancel the sleep command with ctrl+c the string "string interrupted!" will show.. Of course this example is not supposed to be exciting at all, but think of the possibilities! You dont necessarily have to work with true or false variables, you can just act depending on the previous command's state.

Keine Kommentare:

Kommentar veröffentlichen