You are not logged in.

#1 2012-08-29 14:05:22

zenten
Member
Registered: 2012-03-27
Posts: 25

[SOLVED] Trying to pass a randomized list of arguments to a command

So right now I have vlc to be my alarm clock in the morning, through a cron job.  It plays video file I have really loud, and that wakes me up.  However, I have to keep on changing which video file to use, because I find myself getting used to whatever is selected after a few days.

So what I want to do is have it randomly play a video file, with a different one each morning.

What I tried doing was running the command:

ls ~/Desktop/ | shuf | vlc -f --volume=1024 &

in my crontab (I removed the earlier parts of the line to prevent confusion), but that doesn't work.  I think the issue is that vlc tries to directly play the output of shuf, and it can't, since it plays video files, not text files.

So how can I do this?

Last edited by zenten (2012-08-29 15:05:01)

Offline

#2 2012-08-29 14:20:47

progandy
Member
Registered: 2012-05-17
Posts: 5,190

Re: [SOLVED] Trying to pass a randomized list of arguments to a command

What about something like this?

vlc -f  "~/Videos/$(ls -U ~/Videos/ | shuf -n 1)"

| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#3 2012-08-29 14:28:48

zenten
Member
Registered: 2012-03-27
Posts: 25

Re: [SOLVED] Trying to pass a randomized list of arguments to a command

progandy wrote:

What about something like this?

vlc -f  "~/Videos/$(ls -U ~/Videos/ | shuf -n 1)"

OK, I tried that.  It's not working.  Pretty much all the file names have spaces, and it looks like somewhere in all that those are getting escaped in a way that vlc can't handle.

Offline

#4 2012-08-29 14:49:49

zenten
Member
Registered: 2012-03-27
Posts: 25

Re: [SOLVED] Trying to pass a randomized list of arguments to a command

OK, it looks like there were two things going on.  One is that it can't handle spaces (and I haven't figured out a way around that).  The other is that in the first part it doesn't know how to expand the ~.

So I got the following working, but I would still like to be able to have a broader selection than just directories and filenames with no spaces:

vlc -f --volume=1024 "/home/zenten/Desktop/kidstv/My.Little.Pony/$(ls -f ~/Desktop/kidstv/My.Little.Pony/ | shuf -n 1)"

Offline

#5 2012-08-29 14:56:18

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVED] Trying to pass a randomized list of arguments to a command

Parsing the output of ls is generally a bad idea.  It may or may not be the problem here, but it's something you shouldn't get in the habit of doing in any case.

If possible, try something like:

songs=(/path/to/Videos/*)
vlc -f "$( printf "%s\n" "${songs[@]}" | shuf -n1 )"

Edit: Doh! Better yet:

vlc -f "$( find /path/to/Videos | shuf -n 1 )"

I don't have vlc installed so I can't guarantee will work there.  But both worked with ristretto and gimp on images with spaces in the names.

Last edited by alphaniner (2012-08-29 15:04:15)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#6 2012-08-29 15:04:35

zenten
Member
Registered: 2012-03-27
Posts: 25

Re: [SOLVED] Trying to pass a randomized list of arguments to a command

alphaniner wrote:

Parsing the output of ls is generally a bad idea.  It may or may not be the problem here, but it's something you shouldn't get in the habit of doing in any case.

If possible, try something like:

songs=(/path/to/Videos/*)
vlc -f "$( printf "%s\n" "${songs[@]}" | shuf -n1 )"

Edit: Doh! Better yet:

vlc -f "$( find /path/to/Videos | shuf -n 1 )"

I don't have vlc installed so I can't guarantee will work there.  But both worked with ristretto and gimp on images with spaces in the names.

Thanks, that seems to work!

Offline

Board footer

Powered by FluxBB