[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Need help from bash experts...



On Mon, Feb 11, 2002 at 09:22:06PM -0500, Neal Lippman wrote:

*snipped*
> At a bash prompt, I can issue either:
> cp "/mount/windows/spaced name/*" target
> OR
> cp /mount/windows/spaced\ name/* target
> and all works fine.
> However, from within a bash script, something like:
>
> #!/bin/sh
> sourcedir=/mount/windows/spaced\ name
> cp $sourcedir/* target
>
> fails, because the space isn't properly passed to cp, AND further the
shell
> doesn't do expansion on the wild card in the file name.

I suggest a different aproach, use tar instead of cp!
(for more info try `tar --help' or `info tar'
NO problem with _any_ strange names!
+ you can rename copied files to!

basically the script can look like this:

#!/bin/bash
## if bash is called as ' sh' it works too, but you may get strange errors
## on a system where bash isn't avail:(

# $1 is sourcedir
# $2 is targetdir
# save transfered files to /tmp/filelist for renaming
( cd $1; tar cf - *) | (cd ${2?} && tar xvf -) > /tmp/filelist
#do some error handling, the line aborts if 'cd targetdir' doesn't succeed

# renaming is easier with perl : here an example to rename files with
# some odd characters to an "easy to use" name (not completeat all!)
# your milage may vary...

</tmp/filelist perl -ne'
  chomp; ($new = $_) =~ s/[ ~\x7f-\xa0]/"%".ord $&/eg;
  print  qq(mv -i "$_" "$new"\n)";
'

--gk




Reply to: