(t)csh command line arguments issues

  1. The problem statement, all variables and given/known data:

create a shell script, chExt.sh that takes one or more parameters, where the first is a desired extension and the remainder are names of files to be renamed. For each file in the command line, this script should rename the file, as above, if the file exists or print the error message described in the previous step if the file does not exist.
For example,

ls > crocodile.foo
echo bark > dingo.bar
./chExt.sh dat crocodile.foo bogusName.foo dingo.bar

should result in crocodile.foo being renamed crocodile.dat, an error message "bogusName.foo: No such file", and dingo.bar being renamed dingo.dat.

  1. Relevant commands, code, scripts, algorithms:
#!/bin/csh

set ext = $1
shift

foreach file ( $*  )
# with double quotes show all to one name
# with single quotes foreach: No match.
# without quotes separate king cobra.dat into 2 files
echo "original input" "$file"
set oldName = "$file"
if ( -e "$oldName") then
set newName= (`echo "$oldName" | sed "s/\(.*\)\..*/\1/"`)
set newName = "$newName"."$ext"

if ("$file" != "$newName") then
mv "$file" "$newName"
endif
else
    echo "$file"": No such file"
endif

  1. The attempts at a solution (include all code and scripts):
    Line 6 : foreach file ( $* )
    I need it to be able to read in 'cpp' 'file.dat' 'king cobra.dat'
    as 3 variables, but i can only get it to do 'king' and 'cobra.dat'
    i tried putting double quotes around "$*" in my foreach but that causes the variable to be 'file.dat king cobra.dat' as one long variable name

  2. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Old Dominion University, Norfolk, Virginia, USA, Prof. Zeil, CS252<http://www.cs.odu.edu/~cs252/&gt;

please help ASAP, it's due by tonight and i've been stuck on this for days now.

C shell is infamous for quoting issues like this. In Bourne you would just use "$@" to split on parameters instead of spaces, but csh does not have this.

Instead of using the foreach loop, I'd use a while-loop, so you can just use "$1" and not worry about splitting anywhere.

while ( $# > 0 )
        echo "got $1"
        shift
end

I replaced the foreach loop with
while ( $# > 0 )

and the csh -x debugger stops at that line with:
Illegal variable name.

Was it more to it than just replace foreach with the while?

I even ran just:

#!/bin/csh


while ( $# > 0 )
        echo "got $1"
        shift
end


and still got the same Illegal variable name.

There's nothing wrong with the syntax. Can you post the exact code?

I'm using this version of tcsh:

tcsh 6.17.00 (Astron) 2009-07-10 (x86_64-apple-darwin) options wide,nls,dl,al,kan,sm,rh,color,filec

Also, can you (really, really) ask Professor Zeil why he is teaching you this crap?

I mean... C-Shell is so 1980's! Oh, and it's useless.

I have no idea why he chose c-shell. We could also use sh but csh looked easier.

This ends with Illegal variable name.

#!/bin/csh


while ( $# > 0 )
        echo "got $1"
        shift
end

as well as my full code with the while loop

#!/bin/csh

set ext = $1
shift

while ( $# > 0 )
# comments refer to issues with the forreach loop
# with double quotes show all to one name
# with single quotes foreach: No match.
# without quotes separate king cobra.dat into 2 files
echo "original input" "$1"
set oldName = "$1"
if ( -e "$oldName") then
set newName= (`echo "$oldName" | sed "s/\(.*\)\..*/\1/"`)
set newName = "$1"."$ext"

if ("$1" != "$newName") then
mv "$1" "$newName"
endif
else
    echo "$1"": No such file"
endif
shift
end


If csh is not a requirement - and it's certainly not easier than sh, then I'd use something else. This is in Korn shell, and works in Bash too.

$ cat mySctipt 
#!/bin/ksh

EXT=$1 # new extension in $1
shift
while [ $# -gt 0 ]; do
  if [ ! -f "$1" ]; then          # if the file doesn't exist
    echo "File not found: $1"     # Report error
    shift
    continue
  fi

  echo ${1%.*}.$EXT               # Remove everything after . (the extension) and add the desired extension.
  shift
done 


$ ./myScript dat crocodile.foo bogusName.foo dingo.bar
crocodile.dat
File not found: bogusName.foo
dingo.dat

I tried that and it came back with:

./myScript: 1: $: not found
crocodile.dat
File not found: bogusName.foo
dingo.dat

---------- Post updated at 03:33 PM ---------- Previous update was at 03:04 PM ----------

nevermind, thanks to you both for all the help.... I am finally finished with my CS252 class and will use ksh whenever i have the option over csh in the future.