将 mysql 命令输出重定向到 bash 脚本中的文件时遇到问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4620321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:12:32  来源:igfitidea点击:

trouble redirecting mysql command output to file in bash script

mysqlbash

提问by doneright

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

出于某种原因,bash 脚本不会将以下代码段中 mysql 命令的输出重定向到指定文件。

#!/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

当我用“INTO OUTFILE”替换“>”重定向运算符时,出现访问权限错误

回答by chrisaycock

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