Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 10-03-2011, 03:51 PM   #1
LQ Newbie
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
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.

Old 10-03-2011, 05:32 PM   #2
Senior Member
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
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"
1 members found this post helpful.
Old 10-04-2011, 02:34 AM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
Old 10-05-2011, 10:29 AM   #4
LQ Newbie
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Less outputs ANSI escape sequences sprocketboy Mandriva 2 08-05-2009 09:22 PM
escape sequences in enscript lanco Linux - Newbie 1 03-01-2009 03:32 PM
How to stop terminal escape sequences ? Mike Davies Linux - Newbie 1 08-08-2004 12:37 PM
Xterm escape sequences GrayGh0st Linux - Software 3 07-19-2003 01:10 AM
How do I escape $# in shell scripting? KDE4me Programming 3 06-25-2003 11:35 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration