zsh+OSX: how to cope w spaces in fnames? [PART 2]
am 27.01.2008 20:52:37 von kj
This is a follow-up to a post of mine of a few months ago (message-id:
fii7gq$erf$1@reader1.panix.com). There I wrote:
> I recently switched to OS X for my home computer. One recurrent
> source of headaches is the proliferation in OS X of filenames with
> embedded spaces. This means that many of my habitual shell
> interaction practices fail; e.g. anything like
> % for i ( *.mp3 ) frobnicate $i
> will fail with errors like
> frobnicate: La: No such file or directory
> frobnicate: Cucaracha.mp3: No such file or directory
> etc.
As several responders correctly pointed out at the time, this
shouldn't happen with zsh (which, happily, is the shell I use).
My example, in fact, was chosen a bit too hastily. It is not
globbing that causes me problems but rather command substitutions:
% for i ( $( some_command ) ) frobnicate "$i"
In this case, if the individual results produced by some_command
themselves contain whitespace (as happens often in OS X whenever
these results are filenames), the loop variable will be set to
invalid values.
In my pre-OS X days, I used command substitutions *all the time*.
It's one of my all-time favorite techniques. How can I recover
it (short of ditching OS X or renaming my files, of course)?
TIA!
kj
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.
Re: zsh+OSX: how to cope w spaces in fnames? [PART 2]
am 27.01.2008 21:23:57 von Stephane CHAZELAS
On Sun, 27 Jan 2008 19:52:37 +0000 (UTC), kj wrote:
[...]
> % for i ( $( some_command ) ) frobnicate "$i"
>
> In this case, if the individual results produced by some_command
> themselves contain whitespace (as happens often in OS X whenever
> these results are filenames), the loop variable will be set to
> invalid values.
>
> In my pre-OS X days, I used command substitutions *all the time*.
> It's one of my all-time favorite techniques. How can I recover
> it (short of ditching OS X or renaming my files, of course)?
[...]
IFS=$'\n'
for i ($(some_command)) frobnicate $i
(but note that \n is a valid character in a file name).
Or you could also do:
for i (${(f)"$(some_command)"}) frobnicate $i
(f) is a parameter expansion flag which is a short cut for
ps:\n:
"p" is to an expansion flag to recognise prompt expansion in the
s flag. s:\n: is to split on \n.
--
Stephane
Re: zsh+OSX: how to cope w spaces in fnames? [PART 2]
am 28.01.2008 13:24:42 von kj
In <479ce85d$0$899$ba4acef3@news.orange.fr> Stephane Chazelas writes:
>On Sun, 27 Jan 2008 19:52:37 +0000 (UTC), kj wrote:
>[...]
>> % for i ( $( some_command ) ) frobnicate "$i"
>>
>> In this case, if the individual results produced by some_command
>> themselves contain whitespace (as happens often in OS X whenever
>> these results are filenames), the loop variable will be set to
>> invalid values.
>>
>> In my pre-OS X days, I used command substitutions *all the time*.
>> It's one of my all-time favorite techniques. How can I recover
>> it (short of ditching OS X or renaming my files, of course)?
>[...]
>IFS=$'\n'
>for i ($(some_command)) frobnicate $i
>(but note that \n is a valid character in a file name).
>Or you could also do:
>for i (${(f)"$(some_command)"}) frobnicate $i
>(f) is a parameter expansion flag which is a short cut for
>ps:\n:
>"p" is to an expansion flag to recognise prompt expansion in the
>s flag. s:\n: is to split on \n.
Thanks! I should have known the first option you proposed (although
I'd have probably used something more naive like IFS="\n"), but it
doesn't matter: I like your second solution better because it is
more targeted (no need to worry about the possible unwanted side
effects on frobnicate from having IFS set to $'\n').
Parameter expansion flags are still terra incognita for me... Time
to hit the docs!
kynnjo
--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.