简体   繁体   English

如何使用bash脚本在MySQL中进行值

[英]How to values in MySQL with bash script

I am trying to create a bash script for entering values in to a mySQL database. 我正在尝试创建一个bash脚本,用于将值输入到mySQL数据库中。 The ultimate goal is to gather data about hard drives with smartmontools, but I am getting so many errors I decided to break it down and start very simple. 最终目标是使用smartmontools收集有关硬盘驱动器的数据,但是我遇到了很多错误,我决定将其分解并开始非常简单。 I am a total noob at bash / mySQL. 我是bash / mySQL的总菜鸟。 My test database now only contains a table with USER and DATE. 我的测试数据库现在只包含一个USER和DATE的表。 This is my bash script: 这是我的bash脚本:

USR=$USER
DATE=$(date +%y%m%d)
mysql -hlocalhost -uuser -ppw -Dtest<<EOF INSERT INTO testtbl (USER, DATE) VALUES('$USR', $DATE);
EOF
exit

This is the error message I get: 这是我收到的错误消息:

./ysmartmon: line 4: syntax error near unexpected token `('
./ysmartmon: line 4: `mysql -hlocalhost -uuser -ppw -Dtest<<EOF INSERT INTO testtbl (USER, DATE) VALUES('$USR', $DATE);'

What am I doing wrong? 我究竟做错了什么? Is it this ` I need to get rid off? 是这个吗?我需要摆脱它? But how do I do that? 但是我该怎么做?

Break the line after << EOF , like this: << EOF之后打破这条线,像这样:

mysql -hlocalhost -uuser -ppw -Dtest << EOF
INSERT INTO testtbl (USER, DATE) VALUES('$USR', $DATE);
EOF

This << EOF construct is called a here-document , you can read more about it in man bash . 这个<< EOF结构被称为here-document ,你可以在man bash阅读更多关于它的内容。 To search for this in man bash , type /here-doc and press Enter. 要在man bash搜索此内容,请键入/here-doc并按Enter键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM