aristosv
Posts: 159
Joined: Mon Dec 08, 2014 7:47 pm

shuffling mp3 files with spaces causes problems

Mon Dec 19, 2016 7:06 pm

I am trying to loop and shuffle the mp3 files in a directory so I can play them randomly and continuously. So I created this script.

Code: Select all

#!/bin/bash
# import variables
source "${BASH_SOURCE%/*}/variables"
# loop & shuffle
while true
do
 for audio in $(ls $localaudiomedia/* | shuf)
 do
 echo "playing $audio"
 sudo omxplayer --no-osd $audio > /dev/null
 done
done
When the mp3 file doesn't have any spaces in its name, omxplayer plays the mp3 just fine. When there's a space in the name of the mp3 file, the script tries to play the word before, and the word after the space, as two separate mp3 files.

How can I prevent the script from thinking that there's two files?

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: shuffling mp3 files with spaces causes problems

Mon Dec 19, 2016 7:09 pm

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

tpylkko
Posts: 409
Joined: Tue Oct 14, 2014 5:21 pm

Re: shuffling mp3 files with spaces causes problems

Mon Dec 19, 2016 7:10 pm

This is normally done by escsping the space


User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: shuffling mp3 files with spaces causes problems

Mon Dec 19, 2016 8:03 pm

tpylkko wrote:This is normally done by escsping the space
That works if you know where the space is going to appear. The link I found shows how to do it by mucking about with the IFS variable and doesn't care where or how many spaces exist.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: shuffling mp3 files with spaces causes problems

Mon Dec 19, 2016 8:24 pm

Yes, obviously, you can't "escape the space" when the filename is contained inside a variable.

Secondly, you really don't need to mess with IFS. All you really need to do is to quote the variable reference that is passed to omxplayer. In fact, when shell scripting, the wise word is that you should *always* quote your variables (that is, wrap them in " marks) unless you have a specific reason to do otherwise (and those specific reasons are rare).

Thirdly, there is no reason to use 'sudo' when invoking omxplayer.

Here's how I would do this:

Code: Select all

#!/bin/bash
cd "/path/to/the place where/the audio files/are kept"
while :;do
    ls | shuf | while read;do
         omxplayer -whatever_options_you_need "$REPLY"
    done
done
If this post appears in the wrong forums category, my apologies.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: shuffling mp3 files with spaces causes problems

Mon Dec 19, 2016 8:43 pm

Addendum to previous. I just re-checked in "man shuf" and it looks like you can replace "ls | shuf" with "shuf -e *", thereby saving a process.

This also makes it easier to make it, say, *.mp3, if there is any chance of there being non-media files present in the directory.
If this post appears in the wrong forums category, my apologies.

ejolson
Posts: 5373
Joined: Tue Mar 18, 2014 11:47 am

Re: shuffling mp3 files with spaces causes problems

Mon Dec 19, 2016 9:06 pm

Martin Frezman wrote:when shell scripting, the wise word is that you should *always* quote your variables
Some would consider it wise to not use spaces in filenames at all. Next thing, people will be making filenames that include tabs. To create a filename with a tab use quotes and <ctrl>-v to escape the tab from the bash command completion. Here is how it looks:

Code: Select all

$ touch "foo    bar"
$ ls
foo?bar
$ rm "foo?bar"
rm: cannot remove `foo?bar': No such file or directory
$ touch "foo?bar"
$ ls
foo?bar  foo?bar
$ ls | cat
foo     bar
foo?bar

tpylkko
Posts: 409
Joined: Tue Oct 14, 2014 5:21 pm

Re: shuffling mp3 files with spaces causes problems

Mon Dec 19, 2016 10:19 pm

I always thought that quoting is escaping, just a different way of noting it... But I believe that it is good advice to use it always as you don't stand to loose anything.

Return to “General discussion”