[Wolves] Arrays within Bash

John Kennedy skebi69 at gmail.com
Thu Jul 28 11:37:17 UTC 2011


On 28/07/11 11:52, Simon Burke wrote:
> Hi guys,
>
> The list has been quiet this recently so I thought I'd put forward
> this conundrum.
> I have a file that is formatted in a similar fashion to:
>
> BP1_FS1='bp1_usr /usr/BP1'
> BP1_FS2='bp1_mnt /mnt/BP1'
> BP1_FS3='bp1_ora /oracle/BP1'
> BP1_FS4='bp1_data /oracle/BP1/data'
>
> The problem is that I need to loop through all the items that start
> with BP1_FS in another script. I have tried to pull them out into an
> array so I can loop through them, but the problem I am having is that
> the the records in the array are being split into "'bp1_usr"
> "/usr/BP1" and so forth.
>
> It seems to be splitting the records at any occurance of a space, but
> I need that space left in, and for everything in the quote marks to be
> a separate records.
>
> Can anyone suggest either a better way of doing this, or how I can get
> around my array problem. Please note that although I'd like to just
> use python, it has to be a bash script.
>
> Regards,
> Simon.
>
> _______________________________________________
> Wolves LUG mailing list
> Homepage: http://www.wolveslug.org.uk/
> Mailing list: Wolves at mailman.lug.org.uk
> Mailing list home: https://mailman.lug.org.uk/mailman/listinfo/wolves
>

Simon,
If you change the Internal Field Separator you can get around the space 
issue:

oldIFS=$IFS
IFS="\n"
<code to parse BP1 lines>
...
</code>
IFS=$oldIFS


Should do the trick. Then you can use the space as a delimiter.
Hope this helps,
John

-- 
John Kennedy



More information about the Wolves mailing list