[prog] assigning variables in sh

Tim King timk at jtse.com
Wed Jan 12 07:13:14 EST 2005


aec <brat at magma.ca> wrote:
> I have a sh script that polls several servers and stores the 
> result in $serverfile, the file looks like this:
> 
> 141.414.184.125:5156
> 237.215.575.155:5157
> 136.315.470.145:6000
> xion.serve.org:5154
> remaag.lidet.net:5555
> showme.bomang.ca:25154
> noel.sleepy.us:5165
> 
> ...
> 
> I want to now, perform an action on each server in the list...

Hi, Angelina. I don't know precisely what your requirements are, but I 
would probably start with something like:

   servers=`cat $serverfile`
   for server in $servers
   do
     some_action $server
   done

Later on, I could reuse the list of servers without re-reading the file:

   for server in $servers
   do
     some_other_action $server
   done

> I would love to end up with a set of variables, $server1 $server2 
> and so on.. this would make further operations later on in the 
> script a cinch.

This might be useful if you need to access an item in the list given an 
index into the list. At this point, I'd start considering Perl instead 
of sh, however.

I hope this helps.
-TimK
-- 
J. Timothy King         team-oriented  object-oriented  Agile
www.jtse.com            C++  Perl  assembly  embedded systems


More information about the Programming mailing list