Operating System - HP-UX
1835065 Members
2182 Online
110073 Solutions
New Discussion

Re: commands while and for , in command line ...

 
SOLVED
Go to solution
Manuales
Super Advisor

commands while and for , in command line ...

Hi ..
why can i not run while or for in command line?

server:user1 25> while true
while: Expression syntax.

other times i can execute follows:

server:user1 25> while true
> do
> ps -fea | grep procs
> sleep 1
> done
....
....
and i can see result ..
...
...


actually i'm using csh with this user ...

thanks Manuales.
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: commands while and for , in command line ...

Hi Manuales:

If you are running a C-shell (csh) then your syntax is wrong. It should look like:

while (1)
echo "hi"
sleep 1
end

That said, I would avoid the 'csh' at all costs. It's deficiencies are discussed here:

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

The Posix (HP-UX default), or the Korn shell are far, far superior.

Regards!

...JRF...
Sandman!
Honored Contributor

Re: commands while and for , in command line ...

Hi Manuales,

Unlike the Korn and POSIX shells, the csh does not have a "true" built in command. That's why it gives you the error. The csh alternative to the "true" builtin is the number 1 i.e.

while 1 == while true

The while command that's running ok has the ksh/POSIX shell syntax because the csh while looks like...

while (expr)
cmds
end

hope it helps!
Jeff Schussele
Honored Contributor

Re: commands while and for , in command line ...

Hi Manuales,

That's because csh does *not* support the "true" & "false" while arguments - the bourne, korn & Posix do however.
You need an actual control_command for csh - that's just one of the reasons I hate csh.

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Manuales
Super Advisor

Re: commands while and for , in command line ...

Hi again !!!

i have worked with while (1) ...
but ... how can i work with "for" sentence?

do i must writte "for (2)",or what?

Thanks, Manuales.
Sandman!
Honored Contributor

Re: commands while and for , in command line ...

Hi Manuales,

The csh does not have a for loop...instead it provides the "foreach" statement:

foreach name (wordlist)
commands
end

cheers!
James R. Ferguson
Acclaimed Contributor

Re: commands while and for , in command line ...

Hi Manuales:

The 'csh' "foreach" loop walks a list of elements, like this:

#!/usr/bin/csh
foreach i (1 3 5 7 9 11)
if ( $i == 3 ) then
continue
else if ( $i == 9 ) then
break
endif
echo "I see ${i}"
end

...

The 'foreach' is different, therefore, from the C-language 'for' statement.

You can use 'continue' and 'break' to skip processing or terminate the loop altogether.
For instance, in the above, we start out as if we are going to do something with the values 1, 3, 5 7, 9 and 11.

The 'continue' skips the value of "3" and the 'break' terminates the loop. Thus, running the snippet above would produce:

I see 1
I see 5
I see 7

Have a look at the manpages for 'csh' using the above example:

http://www.docs.hp.com/en/B2355-60127/csh.1.html

Regards!

...JRF...
Manuales
Super Advisor

Re: commands while and for , in command line ...

THANKS JAMES .. REALLY YOU ARE A GOOD TEACHER !!!!

:0)

Manuales.
James R. Ferguson
Acclaimed Contributor

Re: commands while and for , in command line ...

Hi Manuales:

Thank *you*! I enjoy helping and teaching.

Regards!

...JRF...