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. TIA, Carl |
Try this (untested):
Code:
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" |
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. |
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. Code:
mysql -u root --password="mypass" mydb -e "select * INTO OUTFILE outfilename FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' from store23data"; 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. Code:
"`date '+%m%d%y%H%M%S'`${surveys[$a]}" 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. Code:
mysql -u root --password="mypass" mydb -e "select * INTO OUTFILE '$outfilename' FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' from ${surveys[$a]}"; Carl M. |
All times are GMT -5. The time now is 04:56 PM. |