Nov 7, 2018

How to use MySQL SQL Commands In Shell Scripts

Example 1:


1
2
3
4
5
6
7
8
#!/bin/sh
user="dbuser"
pass="dbpassword"
db="dbnme"
mysql -u "$user" -p"$pass" "$db" -e"
  sql-statement-1;
  sql-statement-2;"
> output.txt

Example 2:


1
2
3
4
5
6
7
8
#!/bin/sh
user="dbuser"
pass="dbpassword"
db="dbnme"
mysql -u "$user" -p"$pass" "$db" <<EOF
  sql-statement-1;
  sql-statement-2;
EOF