Unix Shell - Doing nothing in a while loop.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > Doing nothing in a while loop.





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 Doing nothing in a while loop.
tomasorti@gmail.com

2006-11-22, 7:27 am

Hi.
I have a ksh script with a while loop like this:

#!/bin/ksh
while [[ `ps -e | grep myProg | awk '{ print $1 }'` -eq "" ]]
do
# do nothing
done

and I want to "do nothing" inside the loop.
This example reports error because there is something wrong.

I've already found what it is, and what it's needed,
but it is not a reasoned answer, just a "trial and error" solution.

Did anyone ever tried to do this? Any "reasoned" solution?

Cheers,
Tomas.

PD: If there is no answer, I'll post the solution in 2 weeks.

Radoulov, Dimitre

2006-11-22, 7:27 am

tomasorti@gmail.com wrote:
:: Hi.
:: I have a ksh script with a while loop like this:
::
:: #!/bin/ksh
:: while [[ `ps -e | grep myProg | awk '{ print $1 }'` -eq "" ]]
:: do
:: # do nothing
:: done
::
:: and I want to "do nothing" inside the loop.
:: This example reports error because there is something wrong.


So, you can try:

until [[ "$(ps -ef | grep [m]yProg)" ]] ;
do
:
done


:: I've already found what it is, and what it's needed,
:: but it is not a reasoned answer, just a "trial and error" solution.


Congratulations!


Regards
Dimitre




Janis Papanagnou

2006-11-22, 1:17 pm

tomasorti@gmail.com wrote:
> Hi.
> I have a ksh script with a while loop like this:
>
> #!/bin/ksh
> while [[ `ps -e | grep myProg | awk '{ print $1 }'` -eq "" ]]
> do
> # do nothing


: do nothing

> done
>
> and I want to "do nothing" inside the loop.


But why so complicated? You don't need an awk, you don't need a
subshell `...`, you don't need the comparison or test operation.

until ps -e | grep myProg # or: while ps -e | grep myProg
# depending on what you wait for
do
sleep 5 # changed from ':' to 'sleep' to not waste ressources
done


Janis

> This example reports error because there is something wrong.


A command is expected and a comment is no command.
And : (true) is a command.

>
> I've already found what it is, and what it's needed,
> but it is not a reasoned answer, just a "trial and error" solution.
>
> Did anyone ever tried to do this? Any "reasoned" solution?
>
> Cheers,
> Tomas.
>
> PD: If there is no answer, I'll post the solution in 2 weeks.
>

Radoulov, Dimitre

2006-11-22, 1:17 pm

Janis Papanagnou wrote:
[...]
:: until ps -e | grep myProg # or: while ps -e | grep myProg
:: # depending on what you wait for
:: do
:: sleep 5 # changed from ':' to 'sleep' to not waste ressources
:: done

Yes, of course, "until ps ..." is sufficient and sleep is more appropriate.

Thanks!

Dimitre


tomasorti@gmail.com

2006-11-22, 1:17 pm

You're right, it was too complicated.
The reason is that I started doing the subshell `ps -e | grep myProg |
awk '{ print $1 }'` to get the pid,
that I used for other purposes and as long as I needed different things
my script started to grow,
and I just recycled this from here, that from there, doing copy &
paste...

It's clear that the expression could be simpler, but I didn't realize.
It just worked.
I have looked again my "unix in a nutshell" book and I finally found
':' as Null command in the Bourne & Korn Shell chapter.

Also, doing

while :
do
commands
done

colon means 'true', as you stated.
Thanks for the post,
Tomas.

On Nov 22, 3:59 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:[vbcol=seagreen]
> tomaso...@gmail.com wrote:
>
>
>
> subshell `...`, you don't need the comparison or test operation.
>
> until ps -e | grep myProg # or: while ps -e | grep myProg
> # depending on what you wait for
> do
> sleep 5 # changed from ':' to 'sleep' to not waste ressources
> done
>
> Janis
>
> And : (true) is a command.
>
>
>
>
>
>

Ed Morton

2006-11-22, 1:17 pm

tomasorti@gmail.com wrote:

[ please don't top-post ]

> You're right, it was too complicated.
> The reason is that I started doing the subshell `ps -e | grep myProg |
> awk '{ print $1 }'` to get the pid,


You already got your answer, but FYI you rarely need to use grep and awk
since awk can search for patterns too. This:

ps -e | grep myProg | awk '{ print $1 }'

should be written as just this:

ps -e | awk '/myProg/{ print $1 }'

and since you're in awk, to eliminate unwanted matches you could get
more specific about which space-separated field you want the string
"myProg" to appear in. e.g. if it's field 8:

ps -e | awk '$8 ~ /myProg/{ print $1 }'

Regards,

Ed.
Stachu 'Dozzie' K.

2006-11-23, 7:33 am

On 22.11.2006, Ed Morton <morton@lsupcaemnt.com> wrote:
> tomasorti@gmail.com wrote:
>
> [ please don't top-post ]
>
>
> You already got your answer, but FYI you rarely need to use grep and awk
> since awk can search for patterns too. This:
>
> ps -e | grep myProg | awk '{ print $1 }'
>
> should be written as just this:
>
> ps -e | awk '/myProg/{ print $1 }'
>
> and since you're in awk, to eliminate unwanted matches you could get
> more specific about which space-separated field you want the string
> "myProg" to appear in. e.g. if it's field 8:
>
> ps -e | awk '$8 ~ /myProg/{ print $1 }'


And if one uses GNU procps, then he can use `ps -C myProg ho pid'.

--
<Kosma> Niektórzy lubi± dozziego...
<Kosma> Oczywi¶cie szanujemy ich.
Stanislaw Klekot
Kaz Kylheku

2006-11-24, 7:19 pm

tomasorti@gmail.com wrote:
> Hi.
> I have a ksh script with a while loop like this:
>
> #!/bin/ksh
> while [[ `ps -e | grep myProg | awk '{ print $1 }'` -eq "" ]]


Unnecessary nonportability here. The test could be done with the
regular POSIX compatible [ ] parentheses rather than the special ksh
double [[ ]] syntax.

> do
> # do nothing
> done


Your loop is a terribly poor idea. You are driving the usage of a
processor to 100% while waiting for myProg to show up in the output of
ps.

> and I want to "do nothing" inside the loop.


No, you want a "sleep 1" (or longer).

tomasorti

2006-11-29, 1:17 pm

Thanks for all the posts. I'm happy I learned new things.
Anyway, I didn't want the 'sleep 1' there, because this was a part from
a quick&dirty script
I needed to catch a process and truss it from the beggining, so I
didn't want to sleep any time.
I didn't care the usage of processor since I it just stuck there very
few time.
Anyway, good point.

On Nov 24, 10:56 pm, "Kaz Kylheku" <kkylh...@gmail.com> wrote:[vbcol=seagreen]
> tomaso...@gmail.com wrote:
>
> regular POSIX compatible [ ] parentheses rather than the special ksh
> double [[ ]] syntax.
>
> processor to 100% while waiting for myProg to show up in the output of
> ps.
>

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com