2

Let's use the example:

#!/bin/bash
ssh me@host "mysql -uuser -ppwd -t -e 'select * from mydb.mytable where id='$1''"

Note a problem: a single quote within a single quote of the command itself. I tried escaping it (\') - no luck, says "Unmatched '". Ideas?

1
  • @Jens then it collides with shell quotes. It's a circle. Commented Jan 8, 2016 at 8:20

2 Answers 2

2

Try

#!/bin/bash
echo "select * from mydb.mytable where id='$1';" | ssh me@host mysql -uuser -ppwd -t
Sign up to request clarification or add additional context in comments.

2 Comments

Brilliant! Looks like I don't even need -e in the end.
-e command is to execute the given command. Here the command is piped to mysql via ssh, making it easier to handle quotes.
0

Even @ringo has answered on your exact query but I am posting this due to below reasons-

By ssh you have to share target server with source server. Even there is no need to share the target server with source server via ssh, you can just achieve it by mysql remote connectivity as per below command.

mysql -h <targetServerIp> -uuser -ppwd -e "select * from mydb.mytable where id='$1';"

Note: Make sure this user should have either global permission or from source server on target mysql server.

2 Comments

Unfortunately this has the same issue with quotes.
I am not seeing any quotes issue here. If you are getting any issue then share so that can be taken care...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.