2

for some reason, bash script doesn't redirect output of mysql command in following snippet to designated file.

#!/usr/bin/bash

cmd="select * from foo > '/tmp/sample.txt'"
mysql --user=test --password=test <db name> --host=<hostname> --port=<portname> -e "$CMD"

above script redirect output to console instead of file

 #!/usr/bin/bash

    cmd="select * from foo INTO OUTFILE '/tmp/sample.txt' "
    mysql --user=test --password=test <db name> --host=<hostname> --port=<portname> -e "$CMD"

when I replace ">" redirection operator with "INTO OUTFILE", I'm getting Access permission error

2
  • Are you trying to save /tmp/sample.txt to your local machine or to the hostname where the MySQL instance is running? Commented Jan 6, 2011 at 22:13
  • Please see BashFAQ/050 (avoid putting commands in variables). Commented Jan 6, 2011 at 22:42

1 Answer 1

5

What if you move the redirection operator (>) out of the quotation marks?

cmd="select * from foo"
mysql --user=test --password=test <db name> --host=<hostname> --port=<portname> -e "$cmd" > /tmp/sample.txt
Sign up to request clarification or add additional context in comments.

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.