Unix Shell - for loop variable

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2007 > for loop variable





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author for loop variable
explor

2007-09-20, 7:21 pm

Hi Gurus,
I am trying to write a script which goes to each box and grabs the
netstat # and present it in a file. The issue for me is the
LOGGED_USERS_${HOST} which isn't working. can you please guide me how
should i get this working. The variable isn't expanding correctly in
for loop.

for HOST in 7 8 9 10 11 12 13
do
LOGGED_USERS_${HOST}=$(ssh mailt${HOST} "netstat -an | grep
ESTABLISHED | grep ".143" |wc -l | tr -d ' '")
done

Thanks & Regards

Kenan Kalajdzic

2007-09-20, 7:21 pm

explor <bhaveshah@gmail.com> wrote:
> Hi Gurus,
> I am trying to write a script which goes to each box and grabs the
> netstat # and present it in a file. The issue for me is the
> LOGGED_USERS_${HOST} which isn't working. can you please guide me how
> should i get this working. The variable isn't expanding correctly in
> for loop.
>
> for HOST in 7 8 9 10 11 12 13
> do
> LOGGED_USERS_${HOST}=$(ssh mailt${HOST} "netstat -an | grep


You need an "eval" in front of the variable name:

eval LOGGED_USERS_${HOST}=$(ssh ...)

[...]

--
Kenan Kalajdzic
explor

2007-09-20, 7:21 pm

On Sep 20, 3:48 pm, Kenan Kalajdzic <ke...@cced.ba> wrote:
> explor <bhaves...@gmail.com> wrote:
>
>
> You need an "eval" in front of the variable name:
>
> eval LOGGED_USERS_${HOST}=$(ssh ...)
>
> [...]
>
> --
> Kenan Kalajdzic


Thanks..but i still have an issue

for HOST in 7 8 9 10 11 12 13
do
eval LOGGED_USERS_${HOST}=$(ssh mailhost${HOST} "netstat -an | grep
ESTABLISHED | grep ".143" |wc -l | tr -d ' '")
done

print "Users Logged in Mailt7: ${LOGGED_USERS_$HOST}"

../imap_connection.sh[24]: "Users Logged in Mailt 7: ${LOGGED_USERS_
$HOST}": bad substitution

Icarus Sparry

2007-09-20, 7:21 pm

On Thu, 20 Sep 2007 16:20:30 -0700, explor wrote:

> On Sep 20, 3:48 pm, Kenan Kalajdzic <ke...@cced.ba> wrote:
>
> Thanks..but i still have an issue
>
> for HOST in 7 8 9 10 11 12 13
> do
> eval LOGGED_USERS_${HOST}=$(ssh mailhost${HOST} "netstat -an | grep
> ESTABLISHED | grep ".143" |wc -l | tr -d ' '") done
>
> print "Users Logged in Mailt7: ${LOGGED_USERS_$HOST}"
>
> ./imap_connection.sh[24]: "Users Logged in Mailt 7: ${LOGGED_USERS_
> $HOST}": bad substitution


Well, the HOST variable is probably not set on line 24 in the way that
you expect.

However the main problem is that you have the wrong syntax. You need

eval print "Users Logged in Mailt7: \${LOGGED_USERS_$HOST}"

You may be able to avoid these eval statements if you can use an array
instead. Also you can probably use grep to count, rather than using wc
and tr, and you probably want to quote the "." in the pattern to grep
or else use fgrep, or else use a colon which is the actual character I
think you want to match.

CMD="netstat -tn | grep -c ':143.*ESTABLISHED'"
for HOST in 7 8 9 10 11 12 13
do
LOGGED_USERS[${HOST}]=$(ssh mailhost${HOST} "$CMD" )
print "Users Logged in Mailt7: ${LOGGED_USERS[$HOST]}"
done
explor

2007-09-21, 1:27 pm

On Sep 20, 4:39 pm, Icarus Sparry <use...@icarus.freeuk.com> wrote:
> On Thu, 20 Sep 2007 16:20:30 -0700, explor wrote:
>
>
>
>
>
>
>
>
>
>
> Well, the HOST variable is probably not set on line 24 in the way that
> you expect.
>
> However the main problem is that you have the wrong syntax. You need
>
> eval print "Users Logged in Mailt7: \${LOGGED_USERS_$HOST}"
>
> You may be able to avoid these eval statements if you can use an array
> instead. Also you can probably use grep to count, rather than using wc
> and tr, and you probably want to quote the "." in the pattern to grep
> or else use fgrep, or else use a colon which is the actual character I
> think you want to match.
>
> CMD="netstat -tn | grep -c ':143.*ESTABLISHED'"
> for HOST in 7 8 9 10 11 12 13
> do
> LOGGED_USERS[${HOST}]=$(ssh mailhost${HOST} "$CMD" )
> print "Users Logged in Mailt7: ${LOGGED_USERS[$HOST]}"
> done- Hide quoted text -
>
> - Show quoted text -


Thanks Kenan.. That worked. Thanks for your suggetions.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com