Mailinglist Archive: opensuse (4498 mails)

< Previous Next >
Re: [SLE] bash Script Using Filenames with Embedded Spaces
  • From: Randall R Schulz <rschulz@xxxxxxxxx>
  • Date: Tue, 7 Nov 2006 06:06:38 +0000 (UTC)
  • Message-id: <200611062206.30898.rschulz@xxxxxxxxx>
On Monday 06 November 2006 21:11, Lucky Leavell wrote:
> ...
>
> OK, here is my complete scipt which runs under bash on an Ubuntu 6.06
> system where I have the mp32ogg utility to convert mp3 to ogg files
> which k3b can handle:
>
> for i in *.mp3
> do
> echo $i
> mp32ogg $i
> done
>
> whch works fine if the only craziness is embedded spaces in the file
> name; it failed when there were parentheses but, since that is rare
> in my situation, I can live with it. (Of course, if the single quoted
> $i would work there ... I'm off to try it!)

Just make it a habit to quote variables. Then you won't have to worry
about any characters that appear in the variables. Remember, there is
only one character code forbidden in Unix / Linux file names: the
slash.

% touch "foo bar" "bar foo" "what not"

No quotes on variable reference:

% for spaceFile in *\ *; do ll $spaceFile; done
ls: bar: No such file or directory
ls: foo: No such file or directory
ls: foo: No such file or directory
ls: bar: No such file or directory
ls: what: No such file or directory
ls: not: No such file or directory

With quoted variable reference:

% for spaceFile in *\ *; do ll "$spaceFile"; done
-rw-r--r-- 1 rschulz users 0 2006-11-06 19:12 bar foo
-rw-r--r-- 1 rschulz users 0 2006-11-06 19:12 foo bar
-rw-r--r-- 1 rschulz users 0 2006-11-06 19:12 what not


If the code fragment you gave above works, then what appears to be
spaces are some other character code, not an ASCII space (040 / 0x20).
There are other character codes that make no mark and are not
zero-width. Perhaps your file names use one of them.

To find out, pipe the names into "od -ab" (use a fixed-width font to
view this and note that the numeric codes are octal, not decimal or
hex):

Normal spaces (octal 020):

% ls *\ * |od -ab
0000000 b a r sp f o o nl f o o sp b a r nl
142 141 162 040 146 157 157 012 146 157 157 040 142 141 162 012
0000020 w h a t sp n o t nl
167 150 141 164 040 156 157 164 012


With en-spaces (a space whose width in proportionally spaced fonts is
equal to that of the lower-case 'n') in the file name 'en space':

% ls -lt |head -5
total 5296
-rw-r--r-- 1 rschulz users 0 2006-11-06 21:59 en space
-rw-r--r-- 1 rschulz users 0 2006-11-06 19:12 bar foo
-rw-r--r-- 1 rschulz users 0 2006-11-06 19:12 what not
-rw-r--r-- 1 rschulz users 0 2006-11-06 19:12 foo bar

% echo en space |od -ab
0000000 e n b nul stx s p a c e nl
145 156 342 200 202 163 160 141 143 145 012


Now see how the absence of quote marks does not cause the file named "en 
space" to be interpreted as two arguments:

% for spaceFile in en*; do ll $spaceFile; done
-rw-r--r-- 1 rschulz users 0 2006-11-06 21:59 en space


Character set issues always complicate life...


Lesson: ALWAYS QUOTE VARIABLE REFERENCES WHOSE CONTENT YOU DO NOT FULLY
CONTROL.

(Sorry for shouting. Sometimes it's called for...)


> Thank you,
> Lucky


Randall Schulz

< Previous Next >
Follow Ups