-   Linux - General (
-   -   Shell Scripting escape sequences (

cmillosh 10-03-2011 03:51 PM

Shell Scripting escape sequences
This has been eating me up for hours and I can't seem to get a handle on the escape sequencingy when running a shell script.

I'm actually calling mysql from the command line with the -e option from withing a shell script.

Here's the line where I'm having problems.

mysql -u root --password="mypass" mydb -e "select * INTO OUTFILE $outfilename FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' from ${surveys[$a]}";

It's all on l line.

I can run a simple outfile query without any problems. surveys[$a]is an array that's defined and $outfilename is constucted with the following line.

This returns a good file name and works.
outfilename="`date '+%m%d%y%H%M%S'`${surveys[$a]}"

No matter how I escape the mysql line I get errors left and right in the query. The query is good if I run it without the variable substitutions, but just not all together.

Any help would be appreciated.


kbp 10-03-2011 05:32 PM

Try this (untested):

echo "select * INTO OUTFILE $outfilename FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' from ${surveys[$a]};" | xargs -I command mysql -u root --password="mypass" mydb -e "command"

David the H. 10-04-2011 02:34 AM

Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Can we see exactly how the final command is supposed to look after substitution, and exactly what values the variables contain? A little of the code in context might also help.

I'd like to help out, but I need to know more to be able to replicate what you're doing, as well as what the final target is.

cmillosh 10-05-2011 10:29 AM

First I want to thank Kph for reminding me of the xargs command, I didn't test his solution, but I think it would have of worked.

David the H.
I'm sorry for the not using the code markup and to answer your question the basis for the script is to loop through and array containing table names and is supposed to export data from an isolated server to another server.
The expanded command should look like the following with outfilename the generated filename cat'd to the array index value and store23data once of the values in the array that denotes the table name.

mysql -u root --password="mypass" mydb -e "select * INTO OUTFILE outfilename FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' from store23data";
The above will still fail even though all the variables are expanded, see solution below as to why.

There is an array defined that stores all of the table names

The "outfilename" is generated by using the date utility with parameters for month/day/year/hour/min/sec and the table name direived from the array index as shown below.

"`date '+%m%d%y%H%M%S'`${surveys[$a]}"
The solution to my issue didn't have anything to do with escape sequences, well maybe it did a little, but when using mysql and an outfile query I had to quote the outfile name parameter within the query and wasn't in my original attempt.

The solution is very similar to my original line to query the database putting the results into a file, the difference is the quoted '$outfilename' variable in the command.

mysql -u root --password="mypass" mydb -e "select * INTO OUTFILE '$outfilename' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' from ${surveys[$a]}";
Thanks again,
Carl M.

All times are GMT -5. The time now is 04:33 AM.