While loop in tcsh

  • Follow


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

0
Reply whereismelvin (5) 1/22/2010 6:48:52 AM

On Thu, 21 Jan 2010 22:48:52 -0800 (PST), 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?

Yes.

> cnt = $cnt(`expr($cnt - 1))
             ^              ^
             |              |
You have-----' and missing--'
0
Reply Bit 1/22/2010 6:55:05 AM


On comp.unix.shell, Melvin <whereismelvin@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


-7
Reply Sidney 1/22/2010 9:16:44 AM

On Jan 22, 11:48=A0am, Melvin <whereismel...@gmail.com> wrote:
> Hi,
>
> =A0 I was trying to code a simple while loop in tcsh shell.
>
> #!/bin/tcsh
> set cnt=3D5
> while ($cnt > 0)
> cnt =3D $cnt(`expr($cnt - 1))
> end
>
> However I am getting the following error :
> cnt=3D4: command not found =A0(Error is going on till <ctrl+c> is pressed=
..
>
> Is this an error with the syntax?
>
> Thanks
> Unix baby

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

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


-- Rakesh
0
Reply Rakesh 1/22/2010 6:17:50 PM

In article 
<041cb750-67fb-478e-91de-1ca043e5bca9@a32g2000yqm.googlegroups.com>,
 Melvin <whereismelvin@gmail.com> 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?

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

-- 
Barry Margolin, barmar@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 ***
0
Reply Barry 1/23/2010 12:59:17 AM

Melvin <whereismelvin@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`

4
Reply Maxwell 1/23/2010 1:14:41 PM

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.


-2
Reply Ed 1/23/2010 3:03:34 PM

freelance writer
1
Reply DIXIEDecker26 3/2/2011 11:32:26 PM

On 2011-03-02, DIXIEDecker26 <user@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
Reply bill7345 (150) 3/3/2011 8:05:14 AM

8 Replies
7298 Views

(page loaded in 0.125 seconds)

Similiar Articles:










7/19/2012 8:28:22 PM


Reply: