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
Yes.
> cnt = $cnt(`expr($cnt - 1))
^ ^
| |
You have-----' and missing--'
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
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
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 ***
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`
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.
--
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.