I am trying to create a shell script for a mysqldump
(and mysql
) operation where the password is stored directly in the script. The password contains special characters which need to be handled accordingly. However, I cannot figure out a combination that actually works.
For example, the following works when executed directly on the console:
mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the\$password | mysql -u the_user target_db --password=the\$password
The password contains a $
which is then escaped with \$
. However, the exact same command does not work when put into a shell_script.sh
. When I execute the script, MySQL complains
Access denied for user 'the_user'@'localhost'
So far I have tried the following within the script:
mysqldump -u the_user --hex-blob --add-drop-table source_db --password=the\$password | mysql -u the_user target_db --password=the\$password
mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the\$password" | mysql -u the_user target_db --password="the\$password"
mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the\$password' | mysql -u the_user target_db --password='the\$password'
mysqldump -u the_user --hex-blob --add-drop-table source_db --password="the$password" | mysql -u the_user target_db --password="the$password"
mysqldump -u the_user --hex-blob --add-drop-table source_db --password='the$password' | mysql -u the_user target_db --password='the$password'
None of these seem to escape the password properly. Out of these, only the first one works properly when used directly on the shell.
What am I missing? How do I have to specify the password, so these commands also work from within a shell script?
%
,=
,?
,#
for example. However, out of these I only need to escape the\$
- at least directly on the shell. The%
is at the beginning of the password, if that makes any difference (which it might?).%
isn't special at all.?
might cause problems, though, as it might try to match. Whitespace could also be a problem. Backslashing every problematic character should work, though.[a-zA-Z0-9]
, and still the command does not work when executed via a script. It still only works when executed directly on the command line.ssh
or other additional level of interpretation involved in the script?