doneright
doneright

Reputation: 111

trouble redirecting mysql command output to file in bash script

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

Upvotes: 2

Views: 7787

Answers (1)

chrisaycock
chrisaycock

Reputation: 37930

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

Upvotes: 5

Related Questions