Execute while loop

Hi,

I have a background process running for which I want to know the status continuously.
I want to execute a WHILE loop in the command prompt so that it keeps on displaying in the screen.But, I am getting the following errors :-

hyper20:~ 3> while [1]
while?
while?
hyper20:~ 4> while {1}
while?
while?
hyper20:~ 5> while [ 1 ]
while: Expression Syntax.

Could someone please help me in this ?

Thanks.

Here's the syntax of while loop:

while command_t
do
         command
         command
         ...
done

Where command_t is executed and its exit status tested. If it's zero, the commands enclosed between the do and done are executed. Then command_t is executed again and its exit status tested. If it's zero, the commands enclosed between the do and done are once again executed. This process continues until command_t returns a nonzero exit status. At that point, execution of the loop is terminated. Execution then proceeds with the command that follows the done.

Why not substitute 1 with true and drop the parenthesis, braces, and brackets :slight_smile:

Hi,
Thanks for the suggestion.

I tried droping 1 with the parenthesis , braces and brackets but then it is giving the following error:-

hyper20:~ 15> while true
while: Expression Syntax.
hyper20:~ 16> while false
while: Expression Syntax.
hyper20:~ 17>

My intended execution of steps are:-

while [1]
do
ps -eaf | grep rm
sleep 10
done

But then this is not working when I execute it from the command prompt :frowning:

Could you please help me in this ?

Thanks.

Running

while true ; do echo Bla ; sleep 10 ; done

works fine on ksh/HP-UX and bash/Linux.

What shell / OS are you using, maybe it isn't POSIX compliant.

Hi,

I copy pasted the while line written above but then it is giving "Expression Syntax" error.
I am using /bin/tcsh Shell and OS is Linux 2.4.21-50.

Please let me know if this helps :slight_smile:

Thanks.

Sorry, I don't know much about csh and it's derivates, except that it can't handle the syntax of the POSIX shell (sh, ksh, bash, ...)
If possible, try switching to bash or ksh, as the only examples of csh "programming" are almost but not completely unmaintainable.

Here goes a Small example of a while in C-Shell :

while (1)
   date
   sleep 1
end

Good Luck, and Success !

Botao

Small comment:
while true;do something;sleep 9999;end
Don't you think that "sleep"/"wait"/etc. command is not the best one to use?

Imagine an example:
You have 2 people working. One is digging graves while the second one is putting coffins into it.
You tell the second one to get the coffin into the grave and then take it out and so on... until the first one diggs another grave.
This is the approach with "sleep"/"wait"/etc.
Better approach would be to tell the second one to put a single coffin into the grave and then to wait until another grave is digged - until then the second man can do whatever he likes.
This corrected approach is called event-driven approach.