[英]export mysql table data to csv using bash script
I am trying to export my sql table data to csv but have these errors.我正在尝试将我的 sql 表数据导出到 csv 但有这些错误。 I am really confused with double quotes and single quotes.
我真的很困惑双引号和单引号。
#!/bin/bash
mysql -u root -pH0tjava1 -B -e 'SELECT CONCAT("sshpass -p ""Password"" rsync -avvtzh -e ""ssh -o StrictHostKeyChecking=no"" --log-file=""/home/toor/rsync2.log""", login,"@", ftp_addr, " :", camera_name,"/", "/",`\ 'home`',"/",login, "/", camera_name) INTO OUTFILE '/tmp/rsynctest3.csv' lines terminated by '\r\n' from inteliviz.cameras;"
Errors:
/usr/local/bin/rsync.sh: line 8: syntax error near unexpected token `)'
/usr/local/bin/rsync.sh: line 8: ` mysql -u root -pH0tjava1 -B -e "select CONCAT ("sshpass -p "Pa55word" rsync -avvtzh -e "ssh -o StrictHostKeyChecking=no" --log-file= "/home/toor/rsync2.log", login,camera_name,ftp_addr) INTO OUTFILE '/tmp/rsynctest3.csv' lines terminated by '\r\n' from inteliviz.cameras;"'
/usr/local/bin/rsync.sh: line 8: unexpected EOF while looking for matching ``'
/usr/local/bin/rsync.sh: line 11: syntax error: unexpected end of file
I am really confused with double quotes and single quotes.
我真的很困惑双引号和单引号。
You have not only double and single quotes, but also backticks in your statement.你的语句中不仅有双引号和单引号,还有反引号。 The mismatches are:
不匹配是:
'SELECT…
and end it with …inteliviz.cameras;"
. To switch the quotes from '
in the first part to "
in the last part, you must insert a closing '
and an opening "
betwixt, eg …camera_name)'" INTO OUTFILE…
'SELECT…
,结束于…inteliviz.cameras;"
切换从报价。 '
在第一部分"
中的最后一部分,则必须插入结束'
和开口"
你我中间,如…camera_name)'" INTO OUTFILE…
`\\ 'home`'
is messed - the first `
is inside the outer single quotes and thus has no special function, the '
before home
closes the single quoted part, the second `
opens an unclosed command substitution, inside of which the final '
is.`\\ 'home`'
被弄乱了 - 第一个`
在外部单引号内,因此没有特殊功能, home
之前的'
关闭单引号部分,第二个`
打开一个未关闭的命令替换,其中最后'
是。 You have to reconsider what you want to have there and get the quote nesting straight.Try with the below script, which works for me:尝试使用以下脚本,这对我有用:
# Read query into a variable
sql="$(cat query.sql)"
# If sqlplus is not installed, then exit
if ! command -v sqlplus > /dev/null; then
echo "SQL*Plus is required..."
exit 1
fi
# Connect to the database, run the query, then disconnect
echo -e "SET PAGESIZE 0\n SET FEEDBACK OFF\n $sql" | \
sqlplus -S -L "$USERNAME/$PASSWORD@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$HOST)(PORT=$PORT))(CONNECT_DATA=(SERVICE_NAME=$DATABASE)))"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.