Shell script foreach help | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Shell script foreach help

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-05-2012
dgrayman dgrayman is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 5 October 2012, 10:52 PM EDT
Location: College Station, TX
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Error Shell script foreach help

I am writing a shell script to uncompress files in a directory, then call a Perl script to search the files for given terms, store those terms in a different output file , and compress the output. I get a syntax error with my use of foreach. Below is my script.

Code:
#!/bin/csh -fxv

if (! $#argv==1); then
    echo "[fail, not enough input arguments]"
    exit 10
else
    echo "the pitch and kinf will be stored in a new file called" $1
    echo " "
endif
rm $1

# uncompress output files in directory
set list= `/bin/ls *.out.gz`
foreach f ($list)
    gunzip $f
end

#run the pearl script on each output file and compress that file
set list= `/bin/ls *.out`
foreach i ($list)
    ./readfile.pl $i $1
    gzip $i
end

exit 0


Last edited by fpmurphy; 10-05-2012 at 11:38 PM..
Sponsored Links
    #2  
Old 10-05-2012
spacebar's Avatar
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 June 2014, 3:23 PM EDT
Location: spaceBAR Central
Posts: 440
Thanks: 0
Thanked 70 Times in 70 Posts
# The shell expands meta-characters, so try this:

Code:
foreach f ( *.out.gz )
  gunzip $f
end

Sponsored Links
    #3  
Old 10-05-2012
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 22 July 2014, 12:11 AM EDT
Location: /dev/ph
Posts: 4,770
Thanks: 59
Thanked 422 Times in 391 Posts
Try replacing

Code:
set list= `/bin/ls *.out`

with

Code:
set list=(`/bin/ls *.out`)

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script: foreach file -> targzip JKMlol Shell Programming and Scripting 12 06-15-2010 05:48 AM
foreach shell question d_kowalske UNIX for Dummies Questions & Answers 10 07-14-2009 06:55 PM
Shell Integer with nested foreach bonesy Shell Programming and Scripting 0 04-29-2009 11:09 PM
C Shell - foreach - No Match error adurga Shell Programming and Scripting 0 04-26-2009 04:53 AM
foreach in shell scripting krisyet UNIX for Dummies Questions & Answers 2 08-11-2006 10:29 AM



All times are GMT -4. The time now is 07:26 PM.