1
#!/bin/bash
mysql_srvr_name="xyz"


function run_msql(){
  x=""
if [ $1  = "use"  ]
then
  x="-e \'$1 $2\'"
echo $x
elif [ $2 = "select"  ]
then
  x="-e \"$1 * from $2;\""
 fi
mysql -h $mysql_srvr_name -P 3306 -u username -p"password" $x


 }

I have created this function in a script file in ubuntu 16.04. whenever i try to run it for example as : run_msql use testdb

it gives me the following error:

ubuntu@xxx:~$ run_msql use testdb
"use testdb"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1049 (42000): Unknown database 'testdb"'

~

Jaymit Desai
  • 153
  • 1
  • 2
  • 11
  • Tip: [ShellCheck](http://www.shellcheck.net) points out issues [like this](https://github.com/koalaman/shellcheck/wiki/SC2089). – that other guy Jun 18 '17 at 18:17
  • Make `x` an array, and store arguments directly, like this: `x=(-e "$1 $2")` and `x=(-e "$1 * from $2;")`, and then use the array like this: `mysql ... "${x[@]}"` – janos Jun 18 '17 at 18:26

0 Answers0