[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OT: Getting MySQL fields with embedded spaces into array

From: Gerard
Subject: Re: OT: Getting MySQL fields with embedded spaces into array
Date: Thu, 29 Oct 2009 11:49:11 -0400

On Wed, 28 Oct 2009 08:38:07 -0400
Greg Wooledge <address@hidden> replied:

> > I have tried setting:
> >     IFS=$( echo )  
> $() removes all trailing newlines from the output of the command that
> it executes.  You're setting IFS to an empty string.  If you want to
> set IFS to a newline, use this:
> IFS=$'\n'
> Or this:
> IFS='
> '

Are you sure? Using: IFS=$(echo) seems to set IFS to a newline here.
> > I have been exploring different hacks to make this work. Perhaps
> > writing to a file and then using 'READ' to put the data into an
> > array.  
> 'read' is the most flexible way, though you don't need a temporary
> file to do this.  I have some more documentation on this approach
> here: http://mywiki.wooledge.org/BashFAQ/005

I got some great ideas from your page. However, I have not been able
to figure out how to save the results of the MySQL search, one that
might include spaces in the data, and inset it into an array without
creating a temp file and then using read to put it into an array.

Using a few suggestions from your page, I created this code snippet. It
works as I expect it to.


## Connect to the SQL server and store the contents of the query in an array

SIGS=$(mysql ${COM_LINE} -e"use ${DB}; SELECT sig from ${table} WHERE 

## Set IFS = line feed or else the array will not load correctly

## Place the elements into a file
printf "%s\n" ${SIGS} > "Sigs.tmp"

## Restore the old IFS setting

## Place the elements into an array & clean any variables
unset i SIGS_ARRAY
while read -r; do SIGS_ARRAY[i++]=$REPLY; done < "Sigs.tmp"

//end snippet//

The array is now loaded and works in my script. I would love to
accomplish this without a temporary file; however, I have not found a
bullet proof method of doing it.



Q:How much does it cost to ride the Unibus?
A:2 bits.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]