Lucid Lucid - 27 days ago 8
MySQL Question

How to values in MySQL with bash script

I am trying to create a bash script for entering values in to a mySQL database. 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. I am a total noob at bash / mySQL. My test database now only contains a table with USER and DATE.
This is my bash script:

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?

Answer

Break the line after << EOF, like this:

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. To search for this in man bash, type /here-doc and press Enter.