Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

While loop in tcsh

1,917 views
Skip to first unread message

Melvin

unread,
Jan 22, 2010, 1:48:52 AM1/22/10
to
Hi,

I was trying to code a simple while loop in tcsh shell.

#!/bin/tcsh
set cnt=5
while ($cnt > 0)
cnt = $cnt(`expr($cnt - 1))
end

However I am getting the following error :
cnt=4: command not found (Error is going on till <ctrl+c> is pressed.

Is this an error with the syntax?

Thanks
Unix baby

Bit Twister

unread,
Jan 22, 2010, 1:55:05 AM1/22/10
to

Yes.

> cnt = $cnt(`expr($cnt - 1))

^ ^
| |
You have-----' and missing--'

Sidney Lambe

unread,
Jan 22, 2010, 4:16:44 AM1/22/10
to
On comp.unix.shell, Melvin <wherei...@gmail.com> wrote:
> Hi,
>
> I was trying to code a simple while loop in tcsh shell.
>

Anyone trying to learn to script in a c-shell would have to
be doing simple things, because they are obviously simple.

Try opening your eyes. Almost everyone in the world is
using a POSIX shell, usually bash.

This has got to be a braindead troll jerking the group around.
This sort of stupid, childish game makes them feel superior.

Killfiled.


Sid


Rakesh Sharma

unread,
Jan 22, 2010, 1:17:50 PM1/22/10
to

For math operations we have the @ operator in the c-shell & it's ilk,
like so:

#!/bin/tcsh -f
@ cnt = 5
while ( $cnt > 0 )
echo $cnt
@ cnt--
end


-- Rakesh

Barry Margolin

unread,
Jan 22, 2010, 7:59:17 PM1/22/10
to
In article
<041cb750-67fb-478e...@a32g2000yqm.googlegroups.com>,
Melvin <wherei...@gmail.com> wrote:

You left out the command "set" in the assignment inside the loop.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Maxwell Lol

unread,
Jan 23, 2010, 8:14:41 AM1/23/10
to
Melvin <wherei...@gmail.com> writes:


I suggest several improvements

1) invoke the shell with -f to prevent ~/.cshrc from interfering with
the script.

#!/bin/tcsh -f

2) add spaces around the parentheses to improve portability

while ( $cnt > 0 )

3) there are two ways to decrement cnt:

@ cnt--
set cnt = `expr $cnt - 1`

Ed Morton

unread,
Jan 23, 2010, 10:03:34 AM1/23/10
to

In general, don't write loops in shell unless your moving files around or doing
something with processes, and don't write shell scripts in [t]csh. Search the
archives for why not.

If you tell us what it is you're trying to do we can advise you on the right
approach. If it was just to print the numbers 5 through 1 that would be:

seq 5 -1 1

See - no loop.

Regards,

Ed.


DIXIEDecker26

unread,
Mar 2, 2011, 6:32:24 PM3/2/11
to
freelance writer


Bill Marcum

unread,
Mar 3, 2011, 3:05:14 AM3/3/11
to
On 2011-03-02, DIXIEDecker26 <us...@compgroups.net/> wrote:
> freelance writer
>
wannabe spammer


--
Man is the only creature that seems to have the time and energy to
pump all his sewage out to sea, and then go swimming in it.

0 new messages