Yan-Hua Wang
Yan-Hua Wang

Reputation: 11

how to escape special characters of mariadb password in shell script

I what to write a shell script to loging in to mariadb. The shell script read one password containing special characters(blank, !@) in a ini file.

The OS is Ubuntu 18.04

the ini file as follows:

user=xxx-xxx-xxx
password=xxx /xxx /xx/ !\@

the shell script as follows:

#!/bin/bash
baseDir="$(cd "$(dirname "$0")" && pwd)"
iniPath="$baseDir/backup.ini"
echo "iniPath is $iniPath"

dbUser="$(grep 'user' $iniPath | cut -d '=' -f 2)"
echo "user is $dbUser"

dbPassword="$(grep 'password' $iniPath | cut -d '=' -f 2)"
echo "password is $dbPassword"

mysql -h localhost -u $dbUser -p'$dbPassword' 

if I input the command as follows: mysql -h localhost -u xxxxxx -p'xxx /xxx /xx/ !@' in command line, it loging successfully. But If I execute the shell script, it always results in accessing denied for user.

Have any suggestions? thanks.

Upvotes: 1

Views: 1549

Answers (2)

Honza P.
Honza P.

Reputation: 1242

Have you tried to use: mysql -h localhost -u $dbUser -p'`echo $dbPassword`'? (special character ` is on US like keyboards under esc key left upper corner, it looks like back apostroph) Looks like the variable with password is not correctly "printed" into a mysql command before its run.

Other way I would recommend trying is to use -p"$dbPassword".

Upvotes: 1

wolcen
wolcen

Reputation: 51

FWIW, the issue is that the shell will not interpolate variables into a string surrounded by single quotes. As Honza specified, the double-quotes will work.

See Difference between single and double quotes in Bash for details.

Upvotes: 0

Related Questions