0

Using either the for loop or the pipe (both work with one filename), I need to figure out how to accept unlimited specified files from standard input. I have tried regular expressions, and various wildcard forms. The two main issues I'm running into: only the first file is put through the script or every single file in the directory is put through. This is an assignment for a basic Unix Course and my problem thus far is over-complication. Based on the rest of the semester, there's a simple fix for what I'm wanting to do and here I've spent two hours perusing hundreds of websites and posts making my head spin.

EDIT: The command line prompt would be something like this ~/dir/script currentWord newWord fileName1 fileName2 fileName3

    #!/bin/csh                                                                     
set currentWord=$1
set newWord=$2
set fileName=$3

  if  { grep -q $1 *$3 } then
     sed -i.bak -e "s/$1/$2/g" $3
   else
     echo "The string is not found."
   endif

#grep -q $1 $3 | sed -i.bak -e "s/$1/$2/g" $3
  • 1
    Why csh? Why not bash or some other POSIX shell derivative? – glenn jackman Apr 25 '14 at 16:25
  • What is this code supposed to accomplish? Where does standard input come into the picture? To supply values where you have `$3`? – tripleee Apr 25 '14 at 16:35
  • As I mentioned in the post...homework. (Not trying to fish for someone else to do my work, just being overtly honest!) From everything I've read and that I know about this professor it's his goal to make it as absolutely difficult as possible to pass this class. Also edited post to include the cmd prompt. From standard input you gain all of the variable information. – user3569949 Apr 25 '14 at 16:36
  • http://www.perl.com/doc/FMTEYEWTK/versus/csh.whynot do not write scripts in csh. – Bruce K Apr 25 '14 at 16:42
  • Making you write scripts in `csh` is certainly one way to make it difficult to pass... – chepner Apr 25 '14 at 19:50

1 Answers1

0

You can access the command line arguments using $argv[]. To loop over them but skip the first two, you can use this construct:

foreach file ($argv[3-])
    # do stuff here, eg
    echo $file
end

You shouldn't use csh though, if you have been instructed to do so by your professor I would question this.

Josh Jolly
  • 11,258
  • 2
  • 39
  • 55