Re: OT: what does this expression mean?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



cs@xxxxxxxxxx wrote:
On 13Sep2016 16:54, todd zullinger <tmz@xxxxxxxxx> 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

Attachment: signature.asc
Description: PGP signature

--
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://lists.fedoraproject.org/admin/lists/users@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: http://fedoraproject.org/code-of-conduct
Guidelines: http://fedoraproject.org/wiki/Mailing_list_guidelines
Have a question? Ask away: http://ask.fedoraproject.org
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [EPEL Devel]     [Fedora Magazine]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Desktop]     [Fedora Fonts]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Fedora Sparc]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux