Archive

Posts Tagged ‘Bash’

Last.FM submissions with Audacious

June 9th, 2010 2 comments

Some time ago the good folks behind my favorite media player Audacious decided to abandon in-built support for Last.FM submissions.

The reasoning behind this seems to be that the Last.FM API was becoming a little too much to maintain which I suppose is fair enough in its quest to remain just a music player i.e. not a screen-hogging iTunes clone like so many others.

Heres how to put Last.FM support back into Audacity.

I’m going to assume you are a sensible person and using a Debian based system rather than Fedora. If you are one of these hat loving deviants, substitute ‘yum’ for apt-get below and feel free to voyage though the countless pains required to get Yum to actually do its job.

Installing LastFMSubmitD

I’m not sure on the capitalization of the daemon process LastFMSubmitD so that might be the wrong name entirely.

Anyway, first install pretty much the only component we need:

apt-get install lastfmsubmitd

OPTIONAL – Set up the HTTP_PROXY

For some reason the daemon does not get proxy details right. This is easily fixed by opening /etc/init.d/lastfmsubmitd in your favorite editor and inserting the following line somewhere near the top (I put it after the ‘GROUP=’ line which should be the last bit of the daemon config area).
If you have no idea what a proxy is or why you should be doing this you can probably skip this section.

export http_proxy="http://whatever.stupid.proxy.edu.au:8080/"

Parse Audacious output

Dump the following script somewhere you can find it. For this example I will use /home/user/bin/scrobble

?Download scrobble
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
if [ ! -x '/usr/lib/lastfmsubmitd/lastfmsubmit' ]; then
	echo "LastFMSubmitD does not appear to exist on this machine"
	echo "You can install it using Apt or Yum"
	exit 1
fi
if [ "$#" == 0 ]; then
	echo 'Usage: scrobble "<artist> - <song>" "<length>"'
	exit 1
fi
 
ARTIST=${1%% - *}
SONG=${1##* - }
LENGTH=$[ $2 / 1000 ]
echo "ARTIST = [$ARTIST]"
echo "SONG = [$SONG]"
echo "LENGTH = [$LENGTH]"
 
/usr/lib/lastfmsubmitd/lastfmsubmit --artist "$ARTIST" --title "$SONG" --length "$LENGTH"

Pointing Song Change at your script

First make sure you have the Song Change plugin enabled in Audacious first.

Now point the command Audacious is to run when starting a new song at the script in the following way:

/home/user/bin/scrobble "%s" "%l"

All done. Now any tracks played via Audacious will be passed to the script which will parse the incoming song title into a format that the LastFMSubmitD daemon can use.

Categories: Fixes Tags: , ,

Trash with Bash

March 4th, 2009 6 comments

One of the regrettably unavoidable aspects of the Unix shell like environments is the impedance between what a user says and what a user means. While this is present in all computing environments the sheer power of Unix based command lines make a potential mistake catastrophic.

Who amongst us has not at some point done something like:

> rm * .tmp

or

> rm *>tmp

The former makes the mistake of a space between the dot and the ‘tmp’ part and the latter the accidental holding down of the shift key while pressing the intended full stop.

While searching for an alternative for the I-have-done-this-too-many-times-and-now-its-embarrassing approach of nuking everything with the ‘rm’ command its time i did something about it.

Inserting the following in your ~/.bashrc file will remap the ‘rm’ command to a slightly safer move-to-trash like behavior. Its not comprehensive but it has saved my life on more than one occasion.

?Download .bashrc
1
2
3
4
5
6
7
8
9
10
11
12
13
# Trash support by Matt Carter <m@ttcarter.com
# Source and information: http://hash-bang.net/2009/03/trash-with-bash
function trash() {
	if [ -d "$HOME/.trashcan/$1" ]; then
		# Already exists in bin - remove
		rm -r "$HOME/.trashcan/$1"
	fi
	mv --target-directory=$HOME/.trashcan/ -- "$@"
}
alias "rm=trash"
alias "emptytrash=/bin/rm -rf $HOME/.trashcan/* $HOME/.trashcan/.??*"
alias 'rm!=/bin/rm -r'
mkdir ~/.trashcan 2>/dev/null

Now the command ‘rm’ moves files into ~/.trashcan. You can also use the command ‘rm!’ when you really mean delete immediately and the utility command ‘emptytrash’ to clean everything out.

Categories: HowTo's Tags: ,

Zapping processes in Bash

January 24th, 2009 No comments

The following is a handy little script I wrote which politely asks a collection of matching processes to exit. After a second the process is forcibly killed.

This command is designed as a drop-in replacement for the slightly cryptic pgrep and pkill commands.

?Download zap.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
zap() {
	# Usage: zap [SIGNAL|-NUMERIC|shoot] <fgrep>
	SIGNAL=''
	case "$1" in
		shoot|-0) SIGNAL='SHOOT';;
		term|-15) SIGNAL='TERM';;
		kill|-9) SIGNAL='KILL';;
		hup|-1) SIGNAL='HUP';;
	esac
	if [ "$SIGNAL" != '' ]; then
		shift
	else
		SIGNAL='SHOOT'
	fi
	PROCS=`ps ax -eo pid,comm | fgrep "$1"`
	MODE=0
	for DATA in $PROCS; do
		if [ "$MODE" -eq 0 ]; then
			PID="$DATA"
			MODE=1
		else
			CMD="$DATA"
			MODE=0
			if [ "$SIGNAL" == 'SHOOT' ]; then
				echo -n "Shooting #$PID - $CMD..."
				kill $PID
				sleep 1
				kill -9 $PID
				echo "Shot"
			else
				echo -n "Killing #$PID - $CMD..."
				kill $SIGNAL $PID
				echo "Killed"
			fi
		fi
	done
}

To install it simply dump the above text inside your existing ~/.bashrc file.

Usage is quite simple:

To politely kill all processes (then force-killing if it still does not die) containing the string ‘gnome’:

1
zap gnome

To just kill-forcefully:

1
zap -9 gnome
Categories: HowTo's Tags: