[Gllug] Bash scripting question

ccooke ccookegllug at gkhs.net
Thu Aug 10 18:52:03 UTC 2006


On Thu, Aug 10, 2006 at 06:52:59PM +0100, Dylan wrote:
> Hi All,
> 
> I have the following script fragment:
> 
> tags=( $( vorbiscomment "$1") )
>  for tag in $(seq 0 $((${#tags[@]} - 1)))
>   do
>   echo ${tags[$tag]}
>   done
> 
> which should extract the comments from the filename in $1 into the array 
> $tags. So far so good. Unfortunately I find that the input is split up at 
> spaces and line breaks, meaning that multi-word tags get distributed across 
> more than one array element. Is there a way to get the assignment statement 
> to break the vorbiscomment at line breaks only, or do I need to write a 
> routine to put the broken lines back together?
> 

(Not knowing the mail clients people are using, I'll add this as a
header: In the following, the newline character '\n' should appear 
as a 'backslash-n', not 'n'. If this is not the case on your MTA, 
please read it as '\\n').

The way I'd do that is:

IFS=$'\n' tags=( $( vorbiscomment "$1" ) )
for tag in "${tags[@]}"
do
	echo "$tag"
done

This sets the Input Field Separator to newline (\n) only, just for 
the array assignment line.

(the "${array[@]}" syntax is a way of getting all the values in an
array, automatically applying quotes where needed. If you needed to 
actually operate on the tag number, though, it can be done entirely
within bash as: 

for (( tag = 0; tag < ${#tags[@]}; tag++ ))
do
	echo "${tags[$tag]}"
done

... which is a little cleaner and will run with less load on the 
system.)

-- 
for((P=10**8,Q=P/100,X=320*Q/(`tput cols`-1),Y=210*Q/`tput lines`,y=-105*Q,v=-2\
20*Q,x=v;y<105*Q;x=v,y+=Y));do for((;x<P;a=b=i=k=c=0,x+=X));do for((;a*a+b*b<2*\
P*P&&i++<99;a=((c=a)*a-b*b)/P+x,b=2*c*b/P+y));do :;done;(((j=(i<99?i%16:0)+30)>\
37?k=1,j-=8:0));echo -ne "\E[$k;$j"mE;done;echo -e \\E[0m;done # Charles Cooke
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list