Monday, December 18, 2006

BASH Script Background Music

So I was sitting here at work, writting a massive script to handle the increasing complexity of our subversion repository for my co-worker Sean (of seancode.blogspot.com fame), and the script did so much I didn't know what to call it. For the sake of naming it, I just named it the most rediculously epic sounding thing I could think of . . . ultramega.sh, which, as it happens, is also the name of a PowerMan 5000 song. Sean and I went out for a smoke and I told him that the name of his script was ultramega.sh, after the PowerMan 5000 song, and I joked that i should have my script play the song in the background while it was chugging along . . . as this bad mother of a script takes a long time to do everything that it does. Sean was like . . . AWESOME! Have it start with the script and stop with the script . . . but we need something more epic than UltraMega, have it play "The Final Countdown" by Europe on repeat. Sweet god I thought . . . that is awesome, let's do it! So I did, with an assist from sean, who figured out how to put VLC into quiet mode, crank the volume and have it repeat. So if you've ever wanted epic background music for a script, or wanted to fuck with one of your programmers . . . here's how we pulled it off:

#!/bin/bash
##########################################################
## FinalCountdown.sh
##
## A template for adding background music to your script
##########################################################

#########VLC VARIABLES###########
VLCFILE=~/finalcountdown.mp3
VLCCOMMAND="vlc ${VLCFILE} --volume 500 -q -L"
GREPCOMMAND=$(echo "$VLCCOMMAND" | sed 's/\ /\\ /g')
##################################

# Start VLC
$VLCCOMMAND &

# PUT THE CONTENTS OF YOUR SCRIPT HERE
#
# do some crap
#
# some more crap
#
# finally back to VLC

# stop VLC
GREPRESULT=$(ps aux | grep "$GREPCOMMAND")
GREPRESULT=${GREPRESULT#*\ }
GREPRESULT=${GREPRESULT%%\ *}
kill ${GREPRESULT}

# end script
exit 0

In case you havn't figured it out yet . . . this is for linux, but it should work equally well for all *nix's. I'll go over the script in detail for those of you not fluent in the language of the gods . . . BASH.

First - the variables:

VLCFILE=~/finalcountdown.mp3

This is the location of the song you want to play with VLC.

VLCCCOMMAND="vlc $VLCFILE --volume 500 -q -L"

This is the command we want to execute . . . this is here in case we want to use a non-vlc audio program, it makes the code transparent enough such that you just enter in the command of whatever audio program you want and it will work. Specifically for VLC the volume directive tells VLC to put volume to 500%, which is the max VLC volume, the -q tells VLC to not output much to the terminal, and the -L loops the audio output, so that if our script takes longer than the song, we're not left with an awkward silence.

GREPCOMMAND=$(echo "$VLCCOMMAND" | sed 's/\ /\\ /g')

This variable is less obvious than the first two, but what it does is it optimizes the command string for a grep by replacing all of the spaces with \spaces, so that it works regular expression-wise.

We then simply start VLC in the background by executing the VLCCOMMAND variable followed by the trailing &.

When we want to kill VLC we use ps aux with our optimized GREPCOMMAND variable to grab the one process that we're looking for . . . afterall you don't want to issue killall vlc and kill all the other VLC processes running too . . . that would suck:

GREPRESULT=$(ps aux | grep "$GREPCOMMAND")

We then need to trim all the extra stuff off the result of this to get just the PID . .. we do that with the following two commands:


GREPRESULT=${GREPRESULT#*\ }
GREPRESULT=${GREPRESULT%%\ *}


Then kill the process with the PID that we just grabbed:


kill ${GREPRESULT}


Pretty friggin' sweet huh? We think so . . . for a real laugh, put this in a script on a machine in your data center . . . and drive those techs crazy with the seemingly random Europe rips coming from your boxes at all hours.

1 comment:

Taufeeq Coorg said...

Wow nice.

Visit Coorg, the romantic hill-station of India...

www.CoorgCreek.com