2

i want to play some music files with mplayer randomly. I don't want to switch the player for that, i want to understand what's wrong here.

ThePandaTooth $ ls
file_1.ogg
file_2.ogg
file_3.ogg

I know, the output of ls | sort -R results in a randomized output of filenames. But I can't play them, with the odd reason, it can't find the file. The sorting is irrelevant here.

ThePandaTooth $ mplayer $(ls | sort -R)
Playing file_1.ogg.
Cannot open file 'file_1.ogg': No such file or directory
Failed to open file_1.ogg

Playing file_2.ogg.
Cannot open file 'file_2.ogg': No such file or directory
Failed to open file_2.ogg

EDIT: Mplaying the files with mplayer * works of course.

6
  • Are these dead symlinks? Commented Jan 2, 2016 at 20:37
  • nope. normal/regular files. Commented Jan 2, 2016 at 20:37
  • 1
    Edit in ls -l. Commented Jan 2, 2016 at 20:39
  • 3
    Try mplayer $(\ls | sort -R) I bet you have an alias ls --color=always. Commented Jan 2, 2016 at 20:48
  • 2
    you don't need to unalias it, you can just reference ls with full path: mplayer $(/bin/ls | sort -R) Commented Jan 2, 2016 at 21:01

2 Answers 2

4

It is very likely that the problem is that you have defined an alias in one of *rc files:

alias ls='ls --color=always'

In such case color codes survive pipe lines and mplayer gets filenames surrounded by those codes. You can pass the output of command substitution $() to printf to see what mplayer really receives, e.g.

printf '%q\n' $(ls | sort -R)

You would see something like

$'\E[01;35mFile_1.ogg\E[0m'
$'\E[01;35mFile_2.ogg\E[0m'
$'\E[01;35mFile_3.ogg\E[0m'

Obviously mplayer reports correctly 'No such file or directory', and prints full problematic file names including the escape codes, but the shell once again interpret these codes as a color, so you only see names in the output what can be confusing.

To pass filenames correctly just run \ls or command ls or even start new shell with bash -f, so that bash won't use an alias but native command

$ printf '%q\n' $(\ls | sort -R)
File_1.ogg
File_2.ogg
File_3.ogg
2

I really don't think you should be involving ls or sort at all here. If you want to play the files in a random order, just use the -shuffle option:

mplayer -shuffle *
2
  • Thanks, but I had asked rather for understanding why my approach (which i though should have worked) didn't worked. ==> i wanted primarily understand bash/shell better, than make this problem working. :) Commented Jan 3, 2016 at 13:00
  • 2
    That's fair enough, although I would say that in general you should avoid parsing ls, as it will lead to all sorts of issues aside from the one you've experienced here. For example, if any of your files contained a space or a *, you'd have a whole new set of problems! Commented Jan 3, 2016 at 15:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.