Launch only the command if the previous one worked inside SSH, shell

pipeshellssh

I've got a SHELL script which is using a pipe to separate my two commands:

ssh -oBatchMode=yes user@hostname "mysql -u yop -pyop -c yop | echo test"

The problem is even if my connection to MySQL doesn't work, it does the echo test.
I would like to forbid my script to perform any command if the previous command doesn't work.

I search for a solution using if but I couldn't come up with anything.

Best Answer

ssh -oBatchMode=yes user@hostname "mysql -u yop -pyop -c yop && echo test"

The && operator executes the second command if and only if the first one succeeds. You can read it as "and then".

By the way, if you just wanted to execute the commands sequentially, you would use a semi-colon ;, as in cmd1; cmd2. A pipe | runs two commands in parallel with stdout of the first connected to stdin of the second. Not at all what you want, in this case.

Related Question