Lucid Lucid - 1 year ago 74
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:

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

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 Source

Break the line after << EOF, like this:

mysql -hlocalhost -uuser -ppw -Dtest << 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download