cs(a)zip.com.au wrote:
On 13Sep2016 16:54, todd zullinger <tmz(a)pobox.com> wrote:
> While we're at it, I might suggest that "${f%.mp4}.mp3" isn't as
> clear as "${f/.mp4/.mp3}" for this particular for loop.
I think that's a little subjective; I personally prefer the former.
You say that like code is expressive subject to the whims of
individual personalities. ;)
> The pattern substitution method ${parameter/pattern/string} will
> also look more familiar to anyone who has used s/pattern/subst/ in
> vim, perl, etc, which is a small bonus.
>
> for f in *.mp4; do
> ffmpeg -i "$f" -c:a libmp3lame "${f/.mp4/.mp3}";
> done
Yeah, until you try to make it robust. Eg against a filename like
"temp4-take9.mp4", which your example will transmute into
"temp3-take9.mp4", not quite the plan. Whereas the "f%.mp4" thing
will behave correctly because of the greedy match.
While I would use /% to be safe, this particular example wouldn't
break without /% because while this ${parameter/pattern/string} syntax
looks like perl, sed, or vim's s/// syntax which all take regular
expressions, bash's does not.
$ f="temp4-take9.mp4"; echo "${f/.mp4/.mp3}"
temp4-take9.mp3
versus
$ f="t.mp4-take9.mp4"; echo "${f/.mp4/.mp3}"
t.mp3-take9.mp4
I tend to feel that glob-based things are better than regexp based
things for filename work, most of the time. Globs are simpler
(albeit less powerful) patterns and shorter and more readable and
often more intuitive. (When they're enough.)
Well said.
I try to avoid being too clever in any sort of coding. That helps for
scripts I don't edit frequently and for code which others need to
maintain with me. There's nothing like scratching your head and
having to read your own code 3 times to figure out what it was doing.
(I think this is a big part of why I moved from perl to python years
ago. Perl is wonderful but it seems to encourage -- or perhaps just
doesn't discourage -- clever idioms. That and I hate having to type
all the sigils and semicolons. ;)
Thanks to all for the question and discussion. I enjoy diversions
like this. I nearly always pick up some new technique and interesting
points of view.
--
Todd
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We hold that what one man cannot morally do, a million men cannot
morally do, and government, representing many millions of men, cannot
do.
-- Auberon Herbert