从 Bash 脚本插入 mysql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39215064/
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-08-31 21:59:32  来源:igfitidea点击:

Insert into mysql from Bash script

mysqlbash

提问by Joshua Hatter

Utilized a few posts here and Google and friends and I'm trying desperately to get something simple to work. I want to insert data into mysql via a bash script.

在这里和谷歌和朋友们使用了一些帖子,我正在拼命尝试让一些简单的工作。我想通过 bash 脚本将数据插入到 mysql 中。

#!/bin/bash -x
DB_USER='jhatter';
DB_PASSWD='XXXXXXX';

DB_NAME='ppr';
TABLE='graph';

#Collect Rank Information from flightaware.com
day=$(date +"%m/%d/%y")
time=$(date +"%H:%M:%S")
rank=$(curl -s http://flightaware.com/adsb/stats/user/jdhatter11 | grep -i site-ranking-29371 | grep window | awk '{print }' | sed s/,//)

#mysql commands
mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME
INSERT INTO $TABLE (`id `, `day`, `time`, `rank`) VALUES (NULL, "$day", "$time", "$rank");
QUIT;

This script when ran manually stops abrubtly after mysql login. It actually brings the login up on screen and I have been inserted in the database. I'm a huge novice and please go easy if it's blatant, but why doesn't the script proceed to process the INSERT and instead throw me into mysql. I envision this all happening in the background...

这个脚本在 mysql 登录后手动运行时突然停止。它实际上在屏幕上显示了登录信息,并且我已被插入到数据库中。我是一个巨大的新手,如果它是公然的,请放轻松,但是为什么脚本不继续处理 INSERT 而是将我扔到 mysql 中。我想象这一切都发生在后台......

回答by janos

You can pass the commands in a here-document, like this:

您可以在here-document 中传递命令,如下所示:

mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME << EOF
INSERT INTO $TABLE (\`id\`, \`day\`, \`time\`, \`rank\`) VALUES (NULL, "$day", "$time", "$rank");
EOF

Notice that the `need to be escaped. I also removed the QUITcommand, as it's unnecessary (good tip @Ven, thanks).

注意`需要转义。我也删除了QUIT命令,因为它是不必要的(好提示@Ven,谢谢)。

Actually, since those column names don't contain special symbols, you don't actually need to quote them, and write the INSERT query a bit simpler, like this:

实际上,由于这些列名不包含特殊符号,因此您实际上不需要引用它们,并且可以更简单地编写 INSERT 查询,如下所示:

mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME << EOF
INSERT INTO $TABLE (id, day, time, rank) VALUES (NULL, "$day", "$time", "$rank");
EOF

回答by Barmar

You need to use a here-doc if you want to put the input to a program after the program in a script.

如果您想在脚本中的程序之后将输入放入程序,则需要使用 here-doc。

mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME <<EOF
INSERT INTO $TABLE (\`id\`, \`day\`, \`time\`, \`rank\`) VALUES (NULL, "$day", "$time", "$rank");
EOF

Note that you need to escape backticks, because they're part of shell syntax to insert the output of a command into the command line.

请注意,您需要转义反引号,因为它们是将命令的输出插入命令行的 shell 语法的一部分。

For a one-liner, you could also use the -eoption.

对于单线,您也可以使用该-e选项。

mysql --user=$DB_USER --password=$DB_PASSWD $DB_NAME -e "NSERT INTO $TABLE (\`id\`, \`day\`, \`time\`, \`rank\`) VALUES (NULL, '$day', '$time', '$rank)"