for loop variable
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > for loop variable




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    for loop variable  
explor


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-21-07 12:21 AM

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






[ Post a follow-up to this message ]



    Re: for loop variable  
Kenan Kalajdzic


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-21-07 12:21 AM

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





[ Post a follow-up to this message ]



    Re: for loop variable  
explor


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-21-07 12:21 AM

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 | gre
p
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_USER
S_
$HOST}": bad substitution






[ Post a follow-up to this message ]



    Re: for loop variable  
Icarus Sparry


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-21-07 12:21 AM

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 | g
rep
> 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_USE
RS_
> $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





[ Post a follow-up to this message ]



    Re: for loop variable  
explor


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
09-21-07 06: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[$HO
ST]}"
> done- Hide quoted text -
>
> - Show quoted text -

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






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:39 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register