I am a new user on Stack Overflow, so I apologize in advance for any potential breaches of site etiquette.
I am attempting to create a BASH script that will generate a command to invoke the MariaDB monitor, i.e. mysql
. I want this mysql
command to include the --init-command
option. I want the --init-command
option to set a list of user variables to a their values, as specified in a configuration file.
The script builds a string that appears to be correct for my purpose but, when it invokes mysql
, an error is generated. If I print out the generated string from the script, it appears to be exactly what I was attempting to create.
I have boiled it down to the following code example:
#!/bin/sh
declare foo="name"
declare bar="value"
declare invoke="mysql -p -D information_schema"
declare opts=" --init-command='SET @$foo:=\"$bar\"'"
invoke+=$opts
echo $invoke
$invoke
When I execute this script, the result looks like:
$ example.sh
mysql -p -D information_schema --init-command='SET @name:="value"'
Enter password:
ERROR 1044 (42000): Access denied for user 'Bill'@'%' to database '@name:="value"''
This error message doesn't even seem to make sense.
However, if I copy the generated command, and paste it back into the command prompt, it requests my password, and proceeds as I would expect, as follows:
$ mysql -p -D information_schema --init-command='SET @name:="value"'
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 171
Server version: 10.3.11-MariaDB Source distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [information_schema]> SELECT @name;
+-------+
| @name |
+-------+
| value |
+-------+
1 row in set (0.000 sec)
MariaDB [information_schema]>
Demonstrating that the SET
command in the --init-command
option was successfully passed to the MariaDB monitor, and executed.
I do not know whether this is a Linux issue, a BASH issue, or a MariaDB issue. So, while I have spent a good amount of time trying to find the answer, I really don't know where the problem originates, and therefore, where to focus my research.
Please note: I only used the information_schema database in my example because I expect that anyone attempting to recreate this problem would have that database available to them.
Thank you in advance for your assistance.