Wednesday, June 18, 2014

bash scripts, success and failure

All this might be trivial, but it's good to have it written down somewhere.


false and true can be used to simulate a command which failed or succeeded(respectively)

false
echo $?
1
true
echo $?
0

You can use || to run a command only if the previous failed:

false || echo "it failed"
it failed 

true || echo "it didn't fail"
(nothing is printed here) 


You can use && to run a command only if the previous succeeded:
true && echo "It succeeded"
It succeeded
false && echo "It succeeded"
(nothing is printed here)  


You can chain as many command as you want with && and || (normally you do this only with &&).
 
set -e  will make sure your script stops execution as soon as a command returns something != 0
 



No comments: