How It Works – A Unix/Linux Command-Line Primer

For those who want to learn a little more about Linux, here’s a step-by-step “decoder ring” of the command:

cat %s | shuffle > $$.spl; mv $$.spl %s; mplayer -fs -playlist %s

First, you should understand that Unix/Linux’s command-line (like most operating system command-lines) give you a tremendous amount of power. In fact, the shell that you run (i.e. bash) has its own programming language that allows powerful programming constructs for variable storage, condition-checking, mathematical calculations and looping. Combine this with other powerful Unix tools like sed, awk, grep, cut and you can actually do quite a lot just in the shell. I say this both to warn you and encourage you to explore the shell and various command-line tools once you’re comfortable enough with them.

Now for this example, we’re not doing anything terribly tricky or impressive, though all those symbols might make it look confusing to a newbie. First off, we’re just telling Myth to execute three statements in the above. The shell determines each statement by the semicolon separators. So, the first statement is:

cat %s | shuffle > $$.spl

cat is the standard Unix command to (con)catenate file(s), the results of which are output. Putting only one filename as an argument to “cat” is the equivalent of asking your computer to print out the contents of that file. For example, if you had a text file named greeting.txt that contained the text “Hello, world!” and you typed:

cat greeting.txt

on the command line, you would see “Hello, world!” printed out and then the next command-line prompt. The reason that the file prints out to your terminal is because, by default, “cat” prints its results to the “standard output”, which is the terminal where the command was invoked. There are other things you can send output to (other than “standard output”) as we shall later see.

The next thing in our first statement is %s. This is a special string which MythVideo recognizes. You should think of this as a special symbol (or “variable”) that holds the filename that you’ve selected in Myth. For instance, if I selected a playlist called “LittleRascals.pls” on the Myth screen, then “cat %s” is equivalent to “cat LittleRascals.pls”.

The next thing in our first statement is a pipe character (|). On my keyboard, to get this symbol I hold shift and press the backslash key (\). This tells Unix/Linux to pipe the output of the first command and feed it as input to a second command. This is an important concept to understand. Think of it as telling the computer to chain commands together like in a factory assembly line. The output of one command becomes the input of the next command. In this example, the output of the “cat” command (your text file) becomes the input of the next command (which is “shuffle”) in the above. (Sidebar: One of the key tenets of Unix is to “do one thing and do it well”. Thus, we have many individual commands like cat, grep, etc that do one thing like print a text file, search for a string, etc. The output of these commands can be chained to other commands to form a larger construct that is more powerful. That’s why the concept of a pipeline is so important).

Anyway, I’m not going to get into the details of how the shuffle AWK script works as it’s a lot more involved than I have time for. Though I will take a moment to say that AWK is one of my favourite Unixy-flavour programming languages and I’m always surprised at how much use I can get out of it. I also appreciate its C-like syntax (not surprising, since the “K” in AWK stands for Brian Kernighen, one of the people that Dennis Ritchie worked with when creating the the C programming language). Anyway, the “shuffle” script takes any input text, shuffles its lines and prints out the shuffled lines. Remember: Do one thing and do it well.

Let’s recap our first statement re-construction so far: “cat %s | shuffle”. This is the Bash way of saying “send the playlist text file as input to the shuffle command and print out the shuffled playlist”.

But we don’t just want to print out the playlist, we want to save it as a file so we can later have MPlayer play the file. Thus, the next thing we need to specify is that the output of the shuffle command should be sent to a file and that’s what the “>” character does. The > character says “write output to a filename which I specify and not to the command-line output”. We could have done:

cat %s | shuffle > temp.spl

Which would have shuffled whatever playlist was selected in Myth and wrote the shuffled playlist out to a file temp.slp. However, the problem with doing the above is what if someone else is using Myth at the same time as you? Or what if you already had a file named “temp.spl” or later wanted to create one? In other words, specifying a filename could have later caused you problems that can easily be avoided by using another Unix/Linux trick: $$

You can think of $$ as a way to get a temporary unique number in your shell scripts, though you should also try to understand that it is the process number of whatever process is running your script at this moment. As long as a process is alive, its process number is unique to the Linux/Unix kernel, so this is a somewhat safe way to save a temporary file and to ensure that only this current process will use it. Now we’ve completed the first statement that Myth will run:

cat %s | shuffle > $$.spl

Which is a bash way of saying: Take my playlist, send it to the shuffle command and save the shuffled playlist output in a temporary file.

The next statement is much simpler:

mv $$.spl %s

The mv command is used to move or rename a file. The first argument to the “mv” command is the file that you want to change and the second argument to the “mv” command is the new location or filename. In this case, we’re renaming the temporary file we created in the first statement (the one that contains the shuffled playlist) back to our original file. This actually overwrites our original playlist file, which is ok because it is already a shuffled playlist, so shuffling again and overwriting it is not a problem.

Now we come to the third and final statement that we want Myth to execute:

mplayer -fs -playlist %s

mplayer is the command used to run the video player. The options that we send to MPlayer are:

  • -fs : Tells mplayer to play the video fullscreen
  • -playlist %s : Tells mplayer to play the playlist file that was specified in Myth (this is our shuffled playlist).

There are a host of other options to look at for MPlayer, but those are enough to play the videos.

So to sum up, the long string that we want MythVideo to execute when we’ve selected a .spl file is:

cat %s | shuffle > $$.spl; mv $$.spl %s; mplayer -fs -playlist %s

Which translates to: Take my playlist file, send it to the shuffle command, take that output and save it as a temp file. Then rename that temp file to the original playlist file. Finally, invoke the video player on the playlist.

Hope this rather lengthy article helped someone understand a bit more about the Linux/Unix command-line and some of the power at your fingertips. Or maybe it at least convinced you that reading that command-line and understanding all its funky symbols is within your grasp. Think about this – we’ve changed the way that our Media Center works because Myth allows you to use the command line.

Pages: 1 2

§315 · December 17, 2006 · Linux, Software, Technology, Tips, Video · · [Print]

3 Comments to “Setting Up MythVideo For Playlists”

  1. This is sweet. I have a bunch of short clips that I can do this with! Thanks.

  2. Patrick Domack says:

    I just use /tmp/

    As each frontend is on a different system, and has it’s own /tmp filesystem no chance of them overwriting each other.

    cat %s | shuffle > /tmp/; mplayer -fs -playlist

  3. Tim says:

    Instead of shuffling by the script, You can also use “-shuffle” as an MPlayer option!