1

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.

  • 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" – Honza P. Jan 21 '19 at 10:31
  • It works fine with -p"$dbPassword". Thank you, Honza P. – Yan-Hua Wang Feb 22 '19 at 10:11
  • Cool, I wrote it as an answer for anyone else with similar problem. – Honza P. Feb 26 '19 at 06:51

2 Answers2

1

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"

Honza P.
  • 1,165
  • 1
  • 11
  • 12
0

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.

wolcen
  • 51
  • 5