# Execute while loop

**URL:** <https://community.unix.com/t/execute-while-loop/228305>\
**Category:** UNIX for Advanced & Expert Users\
**Created:** [January 20, 2009, 5:17am UTC](https://community.unix.com/t/execute-while-loop/228305 "2009-01-20T05:17:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shubhranshu](https://community.unix.com/letter_avatar/shubhranshu/32/5_5575768a8748004e209b776fc1b2916d.png) [@shubhranshu](https://community.unix.com/u/shubhranshu)\
**Post date:** [January 20, 2009, 5:17am UTC](https://community.unix.com/t/execute-while-loop/228305/1 "2009-01-20T05:17:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![angheloko](https://community.unix.com/user_avatar/community.unix.com/angheloko/32/1030_2.png) [@angheloko](https://community.unix.com/u/angheloko)\
**Post date:** [January 20, 2009, 7:13am UTC](https://community.unix.com/t/execute-while-loop/228305/2 "2009-01-20T07:13:36Z")

</div>

Here's the syntax of while loop:

```nohighlight
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 🙂

---

<div class="post-metadata">

**Author:** ![shubhranshu](https://community.unix.com/letter_avatar/shubhranshu/32/5_5575768a8748004e209b776fc1b2916d.png) [@shubhranshu](https://community.unix.com/u/shubhranshu)\
**Post date:** [January 20, 2009, 7:34am UTC](https://community.unix.com/t/execute-while-loop/228305/3 "2009-01-20T07:34:29Z")

</div>

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 ☹

Could you please help me in this ?

Thanks.

---

<div class="post-metadata">

**Author:** ![pludi](https://community.unix.com/user_avatar/community.unix.com/pludi/32/1278_2.png) [@pludi](https://community.unix.com/u/pludi)\
**Post date:** [January 20, 2009, 7:59am UTC](https://community.unix.com/t/execute-while-loop/228305/4 "2009-01-20T07:59:44Z")

</div>

Running

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![shubhranshu](https://community.unix.com/letter_avatar/shubhranshu/32/5_5575768a8748004e209b776fc1b2916d.png) [@shubhranshu](https://community.unix.com/u/shubhranshu)\
**Post date:** [January 20, 2009, 8:09am UTC](https://community.unix.com/t/execute-while-loop/228305/5 "2009-01-20T08:09:21Z")

</div>

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 🙂

Thanks.

---

<div class="post-metadata">

**Author:** ![pludi](https://community.unix.com/user_avatar/community.unix.com/pludi/32/1278_2.png) [@pludi](https://community.unix.com/u/pludi)\
**Post date:** [January 20, 2009, 8:38am UTC](https://community.unix.com/t/execute-while-loop/228305/6 "2009-01-20T08:38:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![botao](https://community.unix.com/user_avatar/community.unix.com/botao/32/341_2.png) [@botao](https://community.unix.com/u/botao)\
**Post date:** [January 26, 2009, 9:12am UTC](https://community.unix.com/t/execute-while-loop/228305/7 "2009-01-26T09:12:56Z")

</div>

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

```nohighlight
while (1)
   date
   sleep 1
end

```

Good Luck, and Success !

Botao

---

<div class="post-metadata">

**Author:** ![adderek](https://community.unix.com/letter_avatar/adderek/32/5_5575768a8748004e209b776fc1b2916d.png) [@adderek](https://community.unix.com/u/adderek)\
**Post date:** [January 27, 2009, 7:55pm UTC](https://community.unix.com/t/execute-while-loop/228305/8 "2009-01-27T19:55:10Z")

</div>

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.
