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.

  • Are these dead symlinks? – Michael Homer Jan 2 '16 at 20:37
  • nope. normal/regular files. – toogley Jan 2 '16 at 20:37
  • 1
    Edit in ls -l. – Michael Homer Jan 2 '16 at 20:39
  • 3
    Try mplayer $(\ls | sort -R) I bet you have an alias ls --color=always. – jimmij Jan 2 '16 at 20:48
  • 2
    you don't need to unalias it, you can just reference ls with full path: mplayer $(/bin/ls | sort -R) – Dan Cornilescu Jan 2 '16 at 21:01
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
|improve this answer|||||
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 *
|improve this answer|||||
  • 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. :) – toogley Jan 3 '16 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! – Tom Fenech Jan 3 '16 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.