If you are like me, who starts starts iTunes with default library and realize you have heard the same song 8 times in the last week, here are two one-line-Jukebox scripts for you. The scripts randomly choose and play songs right from the command line.

Following are the steps I took for my OS X, they should work just fine on a Linux/Unix box:

mpg123 is a real time MPEG 1.0/2.0/2.5 audio player that is not a resource hog.

Download the latest version from the website.

Untar the archive

tar -xvf mpg123-1.4.3.tar

Compile and install the player

./configure --prefix=/usr/local/mpg123
make
make install

Add the path to .bash_profile

echo 'export PATH=/usr/local/mpg123/bin:$PATH' >> ~/.bash_profile

Load the new path

source ~/.bash_profile

Test playing music from command-line!

mpg123 ~/Music/Bagho_Bagh_Gippy_Grewal.mp3

Before proceeding make sure you have an mp3 player installed. I have installed lightweight mpg123, however you can use mplayer for Linux as an alternative.

While running the scripts, I pass my ~/Music folder as an argument. Change the path to point to folder where you store your mp3s.

Open a text editor and save this script. I named it shuffle_play.rb. (You need to have ruby installed on your system. Installed on Leopard and most Linux distributions by default.)

#!/usr/bin/env ruby -w
ARGV.sort_by{ rand }.each {|file| system("mpg123 \"#{file}\"")}

To run the jukebox

ruby shuffle_play.rb ~/Music/*.mp3

Update 1: I ported above script to bash, which is even more simple and can run on any Linux or OS X. Lets name the script shuffle_play_shell.sh

#!/usr/bin/env bash
n=0;for songs in "$@" ; do song_array[n]=$songs ;((n++)) ; done; for ((i=0;i<$n;i+=1));do number=$RANDOM; let "number %= $n"; `mpg123 "${song_array[number]}"`; done

To run the jukebox

source shuffle_play_shell.sh ~/Music/*.mp3

Update 2: The second one is not completely random. It will choose n random songs out of n songs. However it does not checks if each song is played at-least once, thus songs may be repeated.