Selecting track(s) with a rofi-MOC script

Hi everybody. This is my first post ever.
Context. MOC is the default player for my local music folder. It runs on external USB output whenever available. Fast, painless and non-intrusive. However, browsing/searching gets tricky when it comes to the external library (more than a thousand of folders/albums in there). Here come "locate" looking at a dedicated database, and "rofi/dmenu" (multi-select enable) for selecting track(s) and adding selection to MOC playlist.
I'm fine with what I could build around that, playing with a short rofi-locate script by "Gotbletu". It runs safe but not without some flaw. Namely when selecting multiple tracks, mocp -ya would throw "Error: Can't stat /path/to/file" on some entry, usually the first one. No consequence as far as I can see other than playlist occasionally either stopping when reaching that flle or the one next to it, or skipping it; and/or mocp displaying relative path and filename instead of tags.
Thanks in advance to whoever could tell me what I am doing wrong, what makes MOC fail to retrieve pathfile? And how to fix it?

-------
#!/bin/bash
# Source: <https://github.com/gotbletu/shownotes/blob/master/rofi_locate.md>
# rofi/dmenu & locate required; MOC server running on USB audio output; rofi adds selected track(s) to playlist.
# Make sure MOC server can be run; must be running before selection; clear playlist if needed:
if [[ ! "$(grep USB /proc/asound/cards)" ]]; then
echo "No USB output."
exit
else
if [[ ! -z $(pgrep mocp) ]]; then
echo "MOC already running!"
elif [[ ! -z "$(grep RUNNING /proc/asound/card1/*p/*/status)" ]]; then
echo "No Way: output is busy"
exit
else
echo "Running MOC"
mocp -S && mocp -c
fi
fi
# Select track(s) in external library:
cd /full/path/to/external/library
playlist="$(locate -d $HOME/.external.db -r "\w+\.\|flac\|mp3\|m4a$" | cut -c25- | rofi -width 100 -dmenu -i -multi-select -p " ♪ ")"
# Prevent MOC from fooling if selection cancelled:
if [[ "$playlist" ]]; then
mocp -ya "$playlist"
else
exit
fi
# Start playing if needed:
if [[ ! "$(mocp -i Q | grep STOP)" ]]; then
mocp -p
echo "Playing new playlist"
else
echo "Added to playlist"
fi
-------

It seems to me that you might want to remove the quotes around $playlist being passed to MOC:

mocp -ya $playlist

Hi.
Thanks. The problem is, removing quotes would break the path if there is any blank space inside, I guess.
The script works well as is, anyway. I use it mainly to play different versions of a title.
The stripped-down version as no impact anymore on MOC when displaying newly added tracks (pipe to cut removed). But"cat path" error keeps showing. I'd just really like to make it cleaner.

The problem is, removing quotes would break the path if there is any blank space inside, I guess.

Now how did I know you would probably be saying this. It's the reason why we don't use characters like spaces inside filenames.

I'd just really like to make it cleaner.

Apart from not using spaces in filenames, the answer would be to ask the 'rofi' maintainers to include an option to escape such characters in filenames.

I don't know what the output format of 'rofi' is, but if it gives each filename on a seperate line you might be able to pipe it through this to do the necessary escaping yourself: sed 's/ /\\ /'g

However, my suggestion of requesting a 'rofi' option still stands.

def fib (n): #!/bin/bash # rofi/dmenu & locate required; rofi adds selected track(s) to playlist. # Make sure MOC is running: if [[ -z $(pgrep mocp) ]]; then sh $HOME/.moc/mocp-tee && mocp -c fi # Select track(s) in external library (if plugged): cd /path/to/external/library || exit playlist="$(locate -d "$HOME/.external.db" -b "" | rofi -width 100 -dmenu -i -multi-select -p " ♪ ")" # Prevent MOC from fooling if selection cancelled: if [[ "$playlist" ]]; then mocp -ya "$playlist" else exit fi

No spaces in filename is the rule, no doubt.
But quotes are mandatory to me because of the "genealogy" of the music collection I'm dealing with, to make it short. Sorry, I should have pointed it out from the beginning.
'ERROR Stat' didn't interfere with the running of the script, still they kept me worrying, digging, adding/removing lines with no result.
(Yes, I'm an enthusiastic but clumsy newbie. Never had to handle 'stderrs' before.)
Piping to rofi works as expected. No more tags or filename flaws in playlist view.
The main command is now back to the single line of its model.
For convenience (auto-play), I surrounded it with two calls to STOP status.
Clearing playlist: is there any other way to prevent Moc from starting again from top of previously added (and played) tracks?
Here's the latest draft. Comments most welcome, I still might be missing something.
Thank you.

#!/bin/bash # Adds selected track(s) to playlist; # locate and rofi-dmenu required. if [ ! -d /path/to/external/music/directory ]; then exit fi if [ -z $(pgrep mocp) ]; then mocp -S fi if [ "$(mocp -i Q | grep STOP)" ]; then mocp -c fi mocp -a "$(locate -d "$HOME/.external.db" -b "" | rofi -threads 0 -width 100 -dmenu -multi-select -i -p " d` ")" 2>/dev/null if [ "$(mocp -i Q | grep STOP)" ]; then mocp -p fi