Execute while loop | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Execute while loop

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-20-2009
shubhranshu shubhranshu is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 6 May 2009, 3:55 AM EDT
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old 01-20-2009
angheloko's Avatar
angheloko angheloko is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 17 April 2009, 10:44 PM EDT
Location: Philippines
Posts: 125
Thanks: 0
Thanked 0 Times in 0 Posts
Here's the syntax of while loop:


Code:
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
Sponsored Links
    #3  
Old 01-20-2009
shubhranshu shubhranshu is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 6 May 2009, 3:55 AM EDT
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
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.
    #4  
Old 01-20-2009
pludi's Avatar
pludi pludi is offline Forum Advisor  
Cat herder
 
Join Date: Dec 2008
Last Activity: 28 March 2014, 8:35 AM EDT
Location: Vienna, Austria, Earth
Posts: 5,522
Thanks: 38
Thanked 335 Times in 308 Posts
Running

Code:
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.
Sponsored Links
    #5  
Old 01-20-2009
shubhranshu shubhranshu is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 6 May 2009, 3:55 AM EDT
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #6  
Old 01-20-2009
pludi's Avatar
pludi pludi is offline Forum Advisor  
Cat herder
 
Join Date: Dec 2008
Last Activity: 28 March 2014, 8:35 AM EDT
Location: Vienna, Austria, Earth
Posts: 5,522
Thanks: 38
Thanked 335 Times in 308 Posts
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.
Sponsored Links
    #7  
Old 01-26-2009
botao's Avatar
botao botao is offline
Registered User
 
Join Date: Mar 2006
Last Activity: 9 December 2013, 8:21 AM EST
Location: brazil
Posts: 86
Thanks: 0
Thanked 3 Times in 3 Posts
A Small Example

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


Code:
while (1)
   date
   sleep 1
end

Good Luck, and Success !

Botao
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Execute Loop in Telnet Dastard Shell Programming and Scripting 2 07-24-2007 11:38 AM
how to execute a while loop for 20 minutes? subhotech UNIX for Dummies Questions & Answers 6 07-05-2007 02:54 AM
loop does not execute in bash script? fedora Shell Programming and Scripting 2 01-16-2007 11:38 AM
Need to execute 2 scripts, wait, execute 2 more wait, till end of file halo98 Shell Programming and Scripting 1 08-01-2006 04:42 PM
Execute commands parallel in a for loop ? networkfre@k Shell Programming and Scripting 3 11-27-2005 06:26 PM



All times are GMT -4. The time now is 05:29 PM.