I want to do something like this:
if cmd1 && cmd2 echo success else echo epic fail fi
How should I do it?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
The pseudo-code in the question does not correspond to the title of the question.
If anybody needs to actually know how to run command 2 if command 1 fails, this is a simple explanation:
cmd1 || cmd2: This will run
cmd1, and in case of failure it will run
cmd1 && cmd2: This will run
cmd1, and in case of success it will run
cmd1 ; cmd2: This will run
cmd1, and then it will run
cmd2, independent of the failure or success of running
Petr Uzel is spot on but you can also play with the magic $?.
$? holds the exit code from the last command executed, and if you use this you can write your scripts quite flexible.
This questions touches this topic a little bit, Best practice to use $? in bash? .
cmd1 if [ "$?" -eq "0" ] then echo "ok" else echo "Fail" fi
Then you also can react to different exit codes and do different things if you like.