Debian iTOps Tube

Friday, April 27, 2012

csh: Parameters in Shell Script Help

I have a program that is designed to take the first parameter (a file extension) and use it to rewrite the file stored in the second paramerter with the new extension. And if the current file does not exist then the program will print an error message "No such file."



For example, my program is chExt.sh and if the first parameter is the extension 'txt' and my second paramter is the current file name 'marcopolo.grp' then the program would rewrite the file to 'marcopolo.txt'



When executing the program like this ./chExt.sh 'txt' 'marcopolo.grp'

the program successfully rewrites the file to 'marcopolo.txt'



When executing the program like this ./chExt.sh 'cpp' 'batman.FIG'

the program successfully rewrites the file to 'batman.cpp'



The problem I am having is if the second parameter is a file name with the special character "$" in the extension...

For example, ./chExt.sh 'bar' 'fo$o.b$r' does not successfully rewrite the file name to what it should be (fo$o.bar). :wall:



My current script is...




Code:




#!/bin/csh

 

set ext="$1"

shift

 

echo the remaining are $*

foreach file (${*:q})

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




Thank you for any feedback.




No comments:

Post a Comment