LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-15-2010, 11:32 AM   #1
bryanvick
LQ Newbie
 
Registered: Oct 2009
Posts: 15

Rep: Reputation: 0
Looping over mysql query results in bash


I am querying a single string column in a table. The string values have spaces in them. I want to loop over each value in bash. I set IFS to split lists on newlines instead of spaces. When I try this, it is splitting the list of results on the actual character 'n', not the newline '\n'.

DATA=`mysql -u root -ppassword --silent 'SELECT name FROM table_a;'`

IFS=$'\n'
for i in $DATA; do
echo "item = $i"
done

unset IFS

These string values in the column:

spam alot
eggs
mandate today
foo

would produce this output from the script:

item = spam alot
eggs
ma
item = date today
foo


Why is IFS=$'\n' splitting on 'n' and not '\n'?
 
Old 09-15-2010, 01:38 PM   #2
14moose
Member
 
Registered: May 2010
Posts: 83

Rep: Reputation: Disabled
Q: have you tried
Quote:
IFS="\n"
(double-quotes expand metacharacters; single quotes don't)
 
Old 09-15-2010, 02:35 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
@14moose: No actually he has the syntax correct. the $'string' structure is designed for expanding backslashed escapes into their ascii equivalents. Just quoting them doesn't do that. The bash man page section on QUOTING details it.

I don't see anything wrong with the script as written, and I don't get the same problem in testing. Are you perhaps using #!/bin/sh instead of #!/bin/bash?

(Edit: I'll bet that's it. Using #!/bin/sh reproduces the behavior. $'string' is a bash-specific function that's not available in posix-compliance mode.)

You can always use an actual newline instead of the string substitution, in any case.
Code:
IFS='
'
BTW, please use [code][/code] tags around your code, to improve readability and to preserve formatting.

Last edited by David the H.; 09-15-2010 at 02:45 PM. Reason: As above, plus minor rewording.
 
Old 09-15-2010, 04:13 PM   #4
suprstar
Member
 
Registered: Aug 2010
Location: Atlanta
Distribution: ubuntu, debian
Posts: 142
Blog Entries: 2

Rep: Reputation: 23
The OP's code worked for me as written. Could he have set his escape character to something else?
 
Old 09-15-2010, 04:23 PM   #5
bryanvick
LQ Newbie
 
Registered: Oct 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Setting the scripts first line to /bin/bash instead of /bin/sh worked. I guess Ubuntu defaults /bin/sh to use Dash, and I was reading the docs for Bash. Thanks for the help.
 
Old 09-15-2010, 08:19 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use a while read loop instead
Code:
mysql ....  | while read -r data
do
   echo "$data"
done
No need to mess with IFS. Prevents from forgetting to reset IFS back
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Variable in Linux Bash for MySQL Query ThaSaltyDawg Linux - Newbie 7 04-12-2016 11:25 PM
I'm trying to get PHP to filter results from a MySQL query!!! Tom "Techno" Earl Programming 4 07-18-2011 06:49 AM
Can MySQL log on via SSH/bash? mysql:x:27:101:MySQL Server:/var/lib/mysql:/bin/bash Ujjain Linux - Newbie 2 04-24-2009 02:21 PM
mysql: trying to limit results seems to expand them -or- need help with a query BrianK Programming 1 04-01-2009 02:09 PM
Query MySQL in Bash return multiple results ThaSaltyDawg Linux - Newbie 1 11-05-2008 06:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:50 PM.

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