Re: help, help scripting masters

From: Arthur Corliss <acorliss@nevaeh-linux.org>
Date: Mon Mar 22 2004 - 09:22:11 AKST

On Sun, 21 Mar 2004 captgoodnight@acsalaska.net wrote:

> Nope, has to be the path complete. I've used all forms of quotes too.
> The script takes the path/folder name, places it in a variable, then uses it within the script for all
> sorts of actions. The only way I've been able to get around this is using a while
> loop
>
> while read line
> do echo "mv" $line
> done < test | sed -e 's/ /\\ /g' -e 's/mv\\/mv/' > renaming_script.sh
> ./renaming_script.sh
>
> then using the new named folder in a proceding variable which has the spaces removed.
> I've tried arrays too
>
> yup="folder with spaces"
> foo=($(echo $yup | sed 's/ /\\ /g'))
> cd `echo -n ${foo[*]}` #nope, [0] only
> cd $(echo -n ${foo[*]}) #nope, [0] only
>
>
>
> but the escapes get removed as soon as the variable is used. I would really like to get rid of the while loop method,
> too messy.
>
> hope that makes sense, kinda hard for me to explain.

I think what's tripping you up is how the variables are interpolated by the
shell (and/or the scripting engine in this case). You've already figured out
one way to force a shell to recognise your intended atomicity (escaping the
spaces), but you need to remember the escapes are a shell hint, they do not
become part of the filename string itself. The other way is through
double/single quotes. Since, in this case, you want to use variable
interpolation you're stuck with double quotes (which isn't a bad thing).

  foo="file with spaces"
  mv "$foo" no-spaces # Works just fine

If you're doing file-globs in loops, the atomicity is preserved in the various
versions of Korne/POSIX/Bourne shells I've tried:

  acorliss@hrolf:~/test$ touch "a bit of space" no-spaces "more spaces"
  acorliss@hrolf:~/test$ ls -l
  total 0
  -rw-r--r-- 1 acorliss users 0 Mar 22 09:10 a bit of space
  -rw-r--r-- 1 acorliss users 0 Mar 22 09:10 more spaces
  -rw-r--r-- 1 acorliss users 0 Mar 22 09:10 no-spaces
  acorliss@hrolf:~/test$ for file in * ; do mv "$file" "$file".bak ; done
  acorliss@hrolf:~/test$ ls -l
  total 0
  -rw-r--r-- 1 acorliss users 0 Mar 22 09:10 a bit of space.bak
  -rw-r--r-- 1 acorliss users 0 Mar 22 09:10 more spaces.bak
  -rw-r--r-- 1 acorliss users 0 Mar 22 09:10 no-spaces.bak
  acorliss@hrolf:~/test$

I'm not entirely sure what your script is doing, but as long as you're passing
arguments in double quotes, you should be fine. There are a few archaic
versions of system utilities that absolutely will not deal with spaces in
filenames, but does anyone know of such a utility still present in modern
Linux?

        --Arthur Corliss
          Bolverk's Lair -- http://arthur.corlissfamily.org/
          Digital Mages -- http://www.digitalmages.com/
          "Live Free or Die, the Only Way to Live" -- NH State Motto
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Mon Mar 22 09:21:51 2004

This archive was generated by hypermail 2.1.8 : Mon Mar 22 2004 - 09:21:51 AKST