0

I'm writing a small bash script to get a random sample of a database table, connecting through ssh.

ssh $SERVER 'mysql -e "SELECT * FROM ${TABLE} WHERE RAND() < ${PROBABILITY} LIMIT ${LIMIT}" -uroot -p ${DATABASE} > temp_dump_file.sql'

I can't get the arguments to be interpreted. The error is:

 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE RAND() <  LIMIT' at line 1

Thanks in advance :)

2 Answers 2

1

As noted by @l0b0, the reason it's failing is because you're using single quotes, so ${TABLE} etc won't be expanded properly. Try swapping your use of quotes:

ssh $SERVER "mysql -e 'SELECT * FROM ${TABLE} WHERE RAND() < ${PROBABILITY} LIMIT ${LIMIT}' -uroot -p ${DATABASE} > temp_dump_file.sql"
Sign up to request clarification or add additional context in comments.

Comments

1

Bash won't expand variables inside single quotes.

Comments

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.