0

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?

1
  • Done. I am using a very simple password for testing, as simple as "mypasswd" Commented Apr 18, 2018 at 15:51

2 Answers 2

1

The use of quotes is to stop the shell interpreting certain punctuation characters, such as space (parameter separator), > (output redirection), * (wild-card in file mask), etc.

Single quotes stop all interpretation of the quoted text, while double quotes allow $ expansion (and some others). The command inside a script is no different from that typed to an interactive prompt.

The IP address has no shell-specific characters, nor has root, so they do not need to be quoted. If your password has shell-specific characters, such as a space, it will need to be quoted, and because it requires $ expansion they need to be double quotes. The -Bse parameter has both space and ;, so it needs quoting, but either will do, since there is no $ expansion.

The variables you are expanding $sqlip and $sqlpass need to be defined within the subshell which executes the script, so they need to be set in the subshell or set as export in the calling shell.

5
  • Hi @afh. In the script I use the shebang that will run the script in a subshell (edited the question). Is that what yo meant? Commented Apr 18, 2018 at 15:11
  • @AJN - No. Scripts always run in a subshell, unless invoked with the source / . command. Commented Apr 18, 2018 at 15:23
  • Where is 172.17.0.2? Is it the same machine as you're running on? Is it on your intranet? Commented Apr 18, 2018 at 15:41
  • It is a container, and there is no issue of connectivity. I tested the command (mentioned in the beginning of the question) from the host console and it works. Commented Apr 18, 2018 at 15:53
  • 2
    Searching for "sql error 2003 HY000 111" gives a lot of solutions, including this one and the following answer. But your command-line and script commands appear to be the same, so I don't see how this can vary in the script, unless you have unexported configuration variables, such as MYSQL_TCP_PORT. Commented Apr 18, 2018 at 15:57
1

It’s possible that there are some characters in the password variable which are treated specially by the shell.

As a rule, it’s best practice to always quote shell variables to avoid unwanted side effects such as token splitting and wildcard expansion. See When is double-quoting necessary? from Unix and Linux Stack Exchange.

mysql -h "$sqlip" -u root -p"$sqlpass" -Bse "create database mydb;"
1
  • Inside the script, I wrapped the variables in double quotes, but gives the same result (Look at the edited question). Commented Apr 18, 2018 at 15:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.