Randall R Schulz wrote:
On Monday 06 November 2006 19:03, Geir A. Myrestrand wrote:
Randall R Schulz wrote:
Geir,
On Monday 06 November 2006 15:40, Geir A. Myrestrand wrote:
...
for FILE in '*.mp3'; do echo ${FILE}; done You should try these things before submitting them. That will only echo (literally) *.mp3. Witness:
% for FILE in '*.mp3'; do echo ${FILE}; done *.mp3
Randall Schulz I did:
chiangmai:/x # env | grep SHELL SHELL=/bin/bash chiangmai:/x # touch "01 Allegro.mp3" chiangmai:/x # touch "02 Adagio un poco mosso.mp3" chiangmai:/x # touch "03 Rondo - Allegro.mp3" chiangmai:/x # ls -l total 0 -rw-r--r-- 1 root root 0 Nov 6 21:55 01 Allegro.mp3 -rw-r--r-- 1 root root 0 Nov 6 21:55 02 Adagio un poco mosso.mp3 -rw-r--r-- 1 root root 0 Nov 6 21:56 03 Rondo - Allegro.mp3 chiangmai:/x # for FILE in '*.mp3'; do echo ${FILE}; done 01 Allegro.mp3 02 Adagio un poco mosso.mp3 03 Rondo - Allegro.mp3
Here's what's happening. You set FILE to (again, literally) *.mp3. There was one iteration of the loop (as evidenced by the fact that all the output was on one line) and since ${FILE} was not quoted, the *.mp3 was expanded as part of executing the echo command.
The result I showed occurred because there were no .mp3 files in the directory where I executed it and I have the "nullglob" shell option (see the shopt command: "help shopt" or "man bash") _unset_. With this option off a glob (e.g., *.mp3) that matches no files is simply passed through to the command unchanged. If I enable nullglob, then the "echo *.mp3" produces no output (other than the newline always generated by echo).
In any event, this is not a solution to the original problem of working with file names with spaces embedded.
This approach works:
% touch "foo bar" "bar foo" "what not"
% for spaceFile in *\ *; do echo "[[$spaceFile]]"; done [[bar foo]] [[foo bar]] [[what not]]
The *\ * in the for loop is just to exclude all the other files in the directory that did _not_ have spaces in their names.
...
--
Geir A. Myrestrand
Randall Schulz
Thanks for the detailed explanation. Shell expansion is not one of my specialities. I usually use find myself when dealing with these type of issues. Two variations with find are: find . -name '*.mp3' -exec echo {} \; find . -name '*.mp3' -exec ls -l {} \; When I first used the for loop I only had one file in the directory, so I did not even notice that it would expand into just one line. -- Geir A. Myrestrand