[comp.unix.questions] C shell question about foreach

6600bori@ucsbuxa.ucsb.edu (Boris Burtin) (06/05/91)

I'm trying to write a csh script that will execute a series of commands for
every directory that exists in /home/pluto.  This script works if I execute it
while I'm logged in, but it gives me a

	syntax error at line 7: `(' unexpected

when I try to run it from crontab.  I'd appreciate any help I can get!  Here's
the code:

--------
#

set people = `ls /home/pluto`

foreach person($people[*])
  echo $person >> /tmp/test.txt
end

--------

	- Boris Burtin
	(6600bori@ucsbuxa.ucsb.edu)

hunt@dg-rtp.rtp.dg.com (Greg Hunt) (06/06/91)

In article <11842@hub.ucsb.edu>, 6600bori@ucsbuxa.ucsb.edu (Boris Burtin) writes:
> I'm trying to write a csh script that will execute a series of commands for
> every directory that exists in /home/pluto.  This script works if I execute
> it while I'm logged in, but it gives me a
> 
> 	syntax error at line 7: `(' unexpected
> 
> when I try to run it from crontab.  I'd appreciate any help I can get! 
> Here's the code:
> 
> --------
> #
> 
> set people = `ls /home/pluto`
> 
> foreach person($people[*])
>   echo $person >> /tmp/test.txt
> end

Boris, I believe the problem is that scripts run via crontab are
executed by sh by default instead of csh.  You need to force the script
to be executed by the csh by adding:

    #!/bin/csh

as the first line of the script, assuming that your system recognizes
this convention.  Otherwise, you'll need to rewrite the script in sh
instead of csh.

Also, I don't think the [*] after $people is doing any good.  $people
is already a space separated list of words, and all the [*] says is
to take all words.  I think just $people will work equally as well.
At least it does on my system with your example.

Enjoy!

-- 
Greg Hunt                        Internet: hunt@dg-rtp.rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC, USA  These opinions are mine, not DG's.

chris@visionware.co.uk (Chris Davies) (06/06/91)

In article <11842@hub.ucsb.edu> 6600bori@ucsbuxa.ucsb.edu (Boris Burtin) writes:
>I'm trying to write a csh script that will execute a series of commands for
>every directory that exists in /home/pluto.  This script works if I execute it
>while I'm logged in, but it gives me a
>	syntax error at line 7: `(' unexpected
>when I try to run it from crontab.  I'd appreciate any help I can get!  Here's

Crontab assumes everything it gets given is sh script (not csh).
Generally on SysV.3 or earlier (assuming even the existance of a
C-shell) you will need to run your script from crontab explicitly with
the C-shell, viz

	csh myscript

where you've called your script "myscript".

You don't need to do this when you execute the script while you're
logged in, since the C-Shell recognises the # on the first line as
assumes it's a C-Shell script.

Purists might argue that scripts should not be written using the
C-shell language, but rather sh (or perl!) should be preferred.

If you're not using AT&T Unix SysV.3 (or so) then things may be a little
different - you didn't say what sort of Unix you were using.

Chris
-- 
         VISIONWARE LTD, 57 Cardigan Lane, LEEDS LS4 2LE, England
    Tel +44 532 788858.  Fax +44 532 304676.  Email chris@visionware.co.uk
-------------- "VisionWare:   The home of DOS/UNIX/X integration" -------------