I need to load a db into a mysql server and the following command works from the shell:
From shell
mysql -h "172.17.0.2" -u "root" -p"mypasswd" -Bse "create database mydb;"
But not from the script, in which the ip and the password are variables passed as arguments:
The script:
#!/bin/bash
set -e
sqlname=$1
sqlpass=$2
sqlip=$3
...
set -x
echo "Creating the database..."
mysql -h $sqlip -u "root" -p$sqlpass -Bse "create database mydb;"
The result of the script:
./myscript.sh mysql1 mypasswd 172.17.0.2
Creating the database...
+ mysql -h 172.17.0.2 -u root -pmypasswd -Bse 'create database mydb;'
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.2' (111)
-- begin edit
Same result for the variables inside double quotes:
#!/bin/bash
...
mysql -h "$sqlip" -u root -p"$sqlpass" -Bse "create database mydb;"
+ echo 'Creating the database...' Creating the database...
+ mysql -h 172.17.0.2 -u root -pmypasswd -Bse 'create database mydb;' ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.2' (111)
-- end edit
Double quotes and singles quotes with variables inside the script confuse me.
I am missing something obvious here about using variables inside and with quotes.
Any hint?