Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Oct 12 '12 at 2:51

This question came from our site for professional and enthusiast programmers.

    
Do you really need to pipe the output of mysql in another command ? –  sputnick Oct 11 '12 at 22:56

1 Answer 1

up vote 5 down vote accepted
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.

share|improve this answer
    
Oh man I tried & and I don't know why I forgot the double & ... Thanks for your quick answer ! –  Namari Oct 11 '12 at 23:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.