Page 1 of 1

shuffling mp3 files with spaces causes problems

Posted: Mon Dec 19, 2016 7:06 pm
by aristosv
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?

Re: shuffling mp3 files with spaces causes problems

Posted: Mon Dec 19, 2016 7:09 pm
by DougieLawson

Re: shuffling mp3 files with spaces causes problems

Posted: Mon Dec 19, 2016 7:10 pm
by tpylkko
This is normally done by escsping the space

Re: shuffling mp3 files with spaces causes problems

Posted: Mon Dec 19, 2016 7:35 pm
by aristosv
thank you

Re: shuffling mp3 files with spaces causes problems

Posted: Mon Dec 19, 2016 8:03 pm
by DougieLawson
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.

Re: shuffling mp3 files with spaces causes problems

Posted: Mon Dec 19, 2016 8:24 pm
by Martin Frezman
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

Re: shuffling mp3 files with spaces causes problems

Posted: Mon Dec 19, 2016 8:43 pm
by Martin Frezman
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.

Re: shuffling mp3 files with spaces causes problems

Posted: Mon Dec 19, 2016 9:06 pm
by ejolson
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

Re: shuffling mp3 files with spaces causes problems

Posted: Mon Dec 19, 2016 10:19 pm
by tpylkko
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.