BASH question

William Park opengeometry-FFYn/CNdgSA at public.gmane.org
Sun Dec 16 16:40:16 UTC 2007


On Sat, Dec 15, 2007 at 07:00:41AM -0500, Madison Kelly wrote:
> Paul King wrote:
> >Why BASH? If you want scripting, this looks like a prime candidate for a 
> >PERL project.
> >
> >If you insist on BASH, then you will need to teach yourself SED and AWK, 
> >which are two good stream-level parsers.
> 
> It's for a client, and I already have a perl and ruby version of the 
> same script. He wants the three versions for comparison (he likes bash 
> scripts, knows perl and is interested in ruby). So I just need to figure 
> out how to make it work.
> 
> I figured I would need to learn sed and awk, but I can't begin that 
> until I can feed each row of the DB query into an array... Hence my 
> question here. :)

No need for 'sed' or 'awk' for that.  Try something like

    ls -l > file
    while read; do
	N=${#x[*]}
	x[$N]=$REPLY
    done < file

which will read line-by-line, preserving the leading and trailing
whitespaces.  If you're brave, then you can try my C extensions
    
    ls -l | tr '\n' '\0' > file
    vset x < file

which will read NUL-terminated strings from files into array or
positional parameter (if you don't give any variable name).

-- 
William Park <opengeometry-FFYn/CNdgSA at public.gmane.org>, Toronto, Canada
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list