From: Maxwell Lol on
Melvin <whereismelvin(a)gmail.com> writes:

> 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?


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`

From: Ed Morton on
On 1/22/2010 12:48 AM, Melvin wrote:
> 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
>

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.