linux - How to have bash shell not execute rest of semicolon list of commands? -
suppose following in bash @ command prompt:
cmd1;cmd2;cmd3
if cmd1
fails how bash not cmd2
.
cmd1 && cmd2 && cmd3
explanation
execute cmd1
. if fails, cmd2
, cmd3
not executed.
why? because false logically anded else equal false, if cmd1
returns false there no need evaluate cmd2
, cmd3
. , reasoning, if cmd1
succeeds , cmd2
fails, don't execute cmd3
.
note
just make things little more confusing, posix systems (like linux , other unix variants) return 0 success , non-zero failure.
so, when failure above
false = non-zero = failure
true = zero = success
why? because numerical value of return code used indicate different failure codes.
for example,
$ ls /root ls: cannot open directory /root: permission denied $ echo $? 2 $ asdf asdf: command not found... $ echo $? 127 $ ls / bin boot data dev etc home lib ... $ echo $? 0
ls
returns "1" minor problems , "2" more serious problems. bash shell returns "127" indicate "command not found", , ls /
returns "0" indicate success.
Comments
Post a Comment