shell scripts and escaped charecters in file names

Mathias Menzel-Nielsen matze at matzsoft.de
Sun Jan 15 09:54:31 PST 2006


Ronny Hippler wrote:

> Hello,
>     I am trying to get the following script to run with no success.
> - ------------------------------------
> #!/bin/csh
> foreach file (\*vol\*)
> mv $file `basename $file .par2`.PAR2
> echo $file
> end
> - ------------------------------------
> and get the following error:
> usage: mv [-f | -i | -n] [-v] source target
> ~       mv [-f | -i | -n] [-v] source ... directory
>
> I know it is because of the spaces and such in the file names. I have
> tried quoting single double and also escaping them but all fails. what
> is the magic? Please cc me off list. thanks!

Hi.

Sorry, dont know the trick for csh -- but for bash there is an env, 
which can be used to override word-split whitespaces:
IFS  (see bash(1))

The following script should do the trick (i have tested it with some 
filenames with spaces in it succesfully):
8<----8<----8<----
#!/bin/sh
IFS="
"
export IFS;
for i in *;
 do
         mv $i `basename $i .par2`.PAR2;
         echo $i;
done;
8<----8<----8<----8<

note how IFS is set to newline

(replace the *-wildchar in the for-line with your fileset)

Hope it helps
greetings,
    Mathias


More information about the freebsd-questions mailing list