Getting foreach to read a parameter with blank space

my program is designed to take the first parameters as extension, then the rest of the parameters as files to be searched for and, if found, modified by the extension. If not found, it prints an error.

Everything is great until: ./chExt.sh 'com' 'king cobra.dat'

where $file splits up the two words into 'king' and 'cobra.dat' then runs them separately. I need it to read as a whole 'king cobra.dat' into $file.

I heard something about using "shift" to get it to read as a whole, but I'm not sure how to implement it.

#!/bin/csh                                                                   \

set ext="$1"
shift

echo the remaining are $*
foreach file ($*)
echo $file
if (-r "$file") then
set newName=`echo "$file" | sed 's/\.[A-Za-z0-9]*$/'".$ext"'/g'`
echo $newName
if ( "$file" == "$newName" ) then
:
else
mv "$file" "$newName"
endif
end
else
echo "$file": No such file
end
endif

Thanks!

According to the csh man page (csh -- C Shell, a shell (command interpreter) with C-like syntax) the syntax should be:

foreach file (${*:q})

which will properly handle parameters with spaces. You are always at the mercy of the person entering the command, inasmuch as they must properly quote the parameters, but when quoted correctly on the command line this should work.

I haven't coded in csh since the early 90s, and have forgotten most of it, so this is based purely on a quick peek at the man page and one small test.

agama, that code foreach file (${*:q}) would fix the issue with the spaces but what if the file extension had the special character "$" in it. For example, what if the file name was foo$e.b$l and I wanted to rewrite it to foo$e.ball using the same script...

Example: ./chExt.sh 'ball' 'foo$e.b$l'

This command would not create the file "foo$e.ball"

Any help with this? Must be one little tweek of the code somewhere.