Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created a shell script that uses information stored in a config file. The issue I am facing is when I try to pass certain values stored in the config file, for use by the shell script, I get a syntax error.

For example:

config.conf

host=localhost
user=user
password=GhR6R3#h]dSq+C74)Jz9CDF6a7^&L[4= (not my real password)
db_name=database

script.sh

# Load config file TODO: make this more secure
source /path/to/config.conf

# MySQL database dump
mysqldump --lock-tables -h $host -u $user -p $password $db_name > dbbackup_`date +"%Y%m%d"`.bak

This is what happens when I attempt to run the script:

$ bash script.sh
Syntax error near unexpected token `)'
`password=GhR6R3#h]dSq+C74)Jz9CDF6a7^&L[4='

The issue seems to be related to certain characters used in my randomly generated password. I have tried wrapping $password in quotes and when that didn't work I did the same with the actual password stored in the config file, but again I had no success.

What's the best way to solve this problem?

share|improve this question
    
try using \ before the special characters in your password – Dababi Dec 15 '16 at 10:01
up vote 3 down vote accepted

Use double quotes as much as you can.

config.conf should be like

host="localhost"
user="user"
password="GhR6R3#h]dSq+C74)Jz9CDF6a7^&L[4="
db_name="database"

And than your command:

mysqldump --lock-tables -h "${host}" -u "${user}" -p"${password}" "${db_name}" > dbbackup_`date +"%Y%m%d"`.bak

and you should be safe.

share|improve this answer
    
This still caused a slight issue: Got error: 1044: Access denied for user 'database'@'localhost' to database 'GhR6R3#h]dSq+C74)Jz9CDF6a7^&L[4=' when selecting the database. I seem to have now solved the problem by using your solution together with using the long form --password="$password" – Mitchell Cash Dec 15 '16 at 11:30
    
I edited the answer to be precise - there shouldn't be a space between -p and password string. Or to use --password as you do. – stderr Dec 15 '16 at 11:35
    
Thank you, this solved the problem. – Mitchell Cash Dec 15 '16 at 11:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.