4

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.

1
  • Do you really need to pipe the output of mysql in another command ? Oct 11, 2012 at 22:56

1 Answer 1

5
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.

1
  • Oh man I tried & and I don't know why I forgot the double & ... Thanks for your quick answer !
    – Namari
    Oct 11, 2012 at 23:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .