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

Re: bash scripting question



Am Son, 2002-11-03 um 04.51 schrieb Neal Lippman:
> I am trying to solve a bash scripting problem, but I cannot figure it
> out.
> 
> I frequently need to execute a command of the form:
> 	for x in {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z); 	do
> 		<do something with each x> ;
> 	done
> 
> This works fine if I actually type out the entire alphabet list on the
> command line as above, but that's sort of a pain. So, I tried setting a
> shell variable to the alphabet string (export alpha="A,B,C,...,Z"), but
> then the command:
> 	for x in {$alpha} ; 
> 	do
> 		echo $x;
> 	done
> winds up printing the string "{A,B,C,...,Z}" rather than each letter on
> a separate line as expected.
> 
> I've tried various versions, including escaping the {} characters, etc,
> using xargs, etc, but I cannot hit upon a sequence that works.
> 
> I also tried writing a program that printed the alphabet string to
> stdout, but same results.
> 
> Can anyone suggest a syntax that would do the trick here?

Your problem ist the wrong setting of the IFS (man bash, search for IFS)

!#/bin/bash

TARGETS="A B C D E F G H I"	#set up list of targets
OLD_IFS="$IFS"			#remember old IFS 
IFS=" "				#set IFS to "space"

for x in $TARGETS
do
	something $x
done

IFS="$OLD_IFS"			#restore old IFS

That's how i do it all the time. There may be a better way, but it Just
Works (TM)
-- 

Matthias Hentges
[www.hentges.net] -> PGP + HTML are welcome
ICQ: 97 26 97 4   -> No files, no URLs

My OS: Debian Woody: Geek by Nature, Linux by Choice



Reply to: