tags:

views:

130

answers:

3

I'm trying to execute this:

$ mysql --user=XXX --password=XXX --batch --skip-column-names \
 -e "SELECT userid, displayname FROM Users" stackoverflowdb | \
split -l 50 -a 5 - "result."

but bash complains about ) 'unexpected' character (i have this character and few other 'weird ones' in my mysql password). I tried to take my password in quotes --password="myweirdpasshere" but then mysql won't login me at all (probably password is incorrect?)

+2  A: 

If you don't give the password on the command line, it will bring up an interactive prompt. That might solve your error. Just use -p instead of --password=XXX.

Of course, if you require unattended access to the script, that's not a very useful answer. It would be more helpful if you could create a minimal example and tell us the exact error that Bash gives you.

Mark Rushakoff
now my hd works intensively so i guess something is happening... thanks
Phil
+3  A: 

Some characters such as $, `, ", and sometimes \ retain their special meaning inside double quotes. If your password contains any of those characters, use single quotes instead. (Unless your password also contains single quotes, in which case you might just drop the quotes altogether and put a \ before each special character.)

mark4o
If you've a lot of special characters, maybe use `'xxxxx'\''yyyy'`; this uses single quotes to suppress all special characters except "`'`"; the sequence "`'\''`" (excluding the double quotes) terminates the current single quoted string, adds a backslashed single quote, and starts a new single quoted string.
Jonathan Leffler
+3  A: 

You should be able to put the password in the $HOME/.my.cnf file, avoiding this issue as well as increasing the security.

Another option to using $HOME/.my.cnf is embedding the configuration file into the script. A comment on the documentation gives examples on this. For your case it should be:

$ mysql --user=XXX --defaults-file <(printf '[client]\npassword=XXX\n') \
--batch --skip-column-names \
-e "SELECT userid, displayname FROM Users" stackoverflowdb | \
split -l 50 -a 5 - "result."
hlovdal
You should do one or the other, I think. It seems as though you are combining two different methods.
Dennis Williamson
Good feedback. Yes, I describe two different, alternative options, but the text does not make that very obvious. I will make it clearer.
hlovdal