Re: OT: bash help

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

 



On 07Jul2013 20:38, Ian Malone <ibmalone@xxxxxxxxx> wrote:
| On 7 July 2013 20:18, Mike Wright <mike.wright@xxxxxxxxxxxxxx> wrote:
| >>> I'm trying to write a bash command to transcode some videos into audios
| >>> but am having trouble with filenames that contain spaces.
| >>>
| >>> ls *flv
| >>>
| >>> returns this:
| >>>
| >>> Jorge Drexler - Al otro Lado del Río.flv
| >>>
| >>> But in a bash for loop it doesn't work.
| >>>
| >>> for f in `ls *flv`; do echo $f; done
[...]
| 
| As you've discovered, spaces in machineable filenames aren't great.
| However for this case you may want to consider:
| for F in *flv ; do echo "$F" ; done
| The expanded *flv items are kept as single tokens. In fact this is why
| "ls *flv" works to start with, it's the shell that expands the *
| matches (globbing), not the ls command. ls just gets the separate file
| names as arguments.

I would like to second Ian's advice here.

Avoid mucking with IFS if you can possibly help it.

His suggestion to just go straight for:

  for f in *.flv
  do
    ... work with "$f" (note the quotes) ...
  done

is the correct one. No mucking about needed at all.

IFS is a recipes for disaster. It has certain very limited uses,
but is usually a code smell indicating you're doing things the wrong
way.

The shell is actually quite good at handling filenames, even those
with spaces and other punctuation, provided you take a little care.

If you are writing a script to work on these things, hacking with
IFS is asking for trouble - it _may_ present apparently correct
behaviour for your current set of files, but it is just moving the
goalposts instead of fixing the problem. Make your script robust
without IFS and it won't break on you in the future.

While we're on the topic of annoying filenames, may I point you at
a few small scripts I find useful for tidying up (disclaimer: yeah,
I wrote them):

  frename https://bitbucket.org/cameron_simpson/css/src/tip/bin/frename
    Renames files using a perl expression. Same functionality as
    Larry Wall's rename script, slightly tweaked.

  __ https://bitbucket.org/cameron_simpson/css/src/tip/bin-cs/__
    Wrapper for frename, replaces spaces with underscores in filenames.
    With no arguments, works on *' '*, so I usually just run the command "__".

  _% https://bitbucket.org/cameron_simpson/css/src/tip/bin-cs/_%
    Wrapper for frename, replaces %xx with its matching character
    and also replaces spaces with underscores in filenames.
    With no arguments, works on *'%'*, so I usually just run the command "_%".
    Very handy for URL downloads than haven't been unmunged.

  lc https://bitbucket.org/cameron_simpson/css/src/tip/bin-cs/lc
    Lowercases filesnames and also does the %xx and spaces thing
    and turns "&" into "+". Really rather overkill.
    Handy for normalising mixed-case file collections if you don't
    want to preserve the case.
    I don't use this as much as I once did.

Anyway, I find __ and _% very handy sometimes.

Cheers,
-- 
Cameron Simpson <cs@xxxxxxxxxx>

Sattinger's Second Law: Even though you've plugged it in, it still won't work
until you switch it on.
-- 
users mailing list
users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe or change subscription options:
https://admin.fedoraproject.org/mailman/listinfo/users
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