Bash scripting : The importance of a sanity check

echo "Execute $somecode ?"
read bogusvar
$somecode
-----

The above might be the most important part of any bash script. It does two things :

1. Shows you the actual command being executed
2. Gives you the chance to cancel the program

I recently learned the importance of this when “rm -rf $variable” became “rm -rf /” when executed. Before I realized what was going on /etc/ and a dozen /home/username/ directories were gone.

Avoid making the same mistake by incorporating some form of a sanity check into all of your bash scripts.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Digg
  • StumbleUpon
  • Technorati
  • Netscape
  • Fark
  • Slashdot

Bash Script : Batch convert mp3

So today I needed a bash script that will use lame to batch covert mp3 files to a lower bitrate so I can squeeze more onto my mp3 player.

After looking around on the net and taking bits of other peoples code (thanks NoStop and keefaz) I came up with this :

Use at your own risk !

Watch out for lame to generate errors. I had some damaged mp3 files and when lame tried to convert them it generated an error then stop processing.

#!/bin/sh    

#Be sure to edit as needed !
#Step 1 - Remove the spaces from the names
#example : Track 1.mp3 will be renamed to Track_1.mp3
#This is done because step 2 can't hand spaces in file names    

for f in *; do
file=$(echo $f | tr ' ' _)
[ ! -f $file ] && mv "$f" $file
done    

#Step 2
#Convert mp3 to a lower quality
#All mp3s are converted to 112 bit using "-b 112"
#All mp3s given names with _112 at the end
#example : Track_1.mp3 will be converted to Track_1_112.mp3
ls -1 *.mp3 | sed "s/(.*).mp3/1.mp3 1_112.mp3/" | xargs -n 2 lame -b 112
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Digg
  • StumbleUpon
  • Technorati
  • Netscape
  • Fark
  • Slashdot

Bash Script : GNOME vs KDE

Answer the age old question.
Download script here.

#!/bin/sh
# Which is better, GNOME or KDE ?
#
# Version : .014 beta
# Last changes : 3/7/2007
# http://linux.blowshard.net
#
# Not for use in a production environment

ver=".013 beta"
wmCheck="GNOME"    #GNOME goes first…
echo
echo "KDE vs GNOME $ver"
echo
while [ "$solved" != "2" ]; do

	if [ "$wmCheck" = "GNOME" ]; then
		other="KDE"
		else
		other="GNOME"
	fi
echo "Does $wmCheck have the features you want ? "
echo -n "(Y)es (N)o (U)nsure : "
read bobloblaw	#Any Arrested Development fans ?
echo

case $bobloblaw in

	[Yy])
	winner=$wmCheck
	loser=$other
	wmCheck=$other
	solved=$(($solved+1))
	happy_test=$(($happy_test+1))
	;;

	[Nn])
	winner=$other
	loser=$wmCheck
	solved=$(($solved+1))
	wmCheck=$other
	happy_test=$(($happy_test+2))
		if [ $happy_test = 4 ]; then
		echo "GNOME vs KDE = No Winner"
		echo "Here are some other window managers :"
		echo "http://en.wikipedia.org/wiki/Window_manager"
		echo
		exit 0
		fi
	;;

	[Uu])

	solved=$(($solved+1))
	wmCheck=$other
	happy_test=$(($happy_test+3))
		if [ $happy_test -gt 5 ]; then
		echo "GNOME vs KDE = No Winner"
		echo "Information about GNOME and KDE :"
		echo "http://en.wikipedia.org/wiki/KDE"
		echo "http://en.wikipedia.org/wiki/GNOME"
		echo
		exit 0
		fi
	;;
esac

done
if [ "$happy_test" -gt 2 ]; then
echo "$winner is better than $loser"
echo
else
echo "GNOME vs KDE = No Winner"
echo
fi
exit 0
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Digg
  • StumbleUpon
  • Technorati
  • Netscape
  • Fark
  • Slashdot

Bash Scripting : sha1sum

When using echo to send text to sha1sum make sure you do it like this :

echo -n “abc123″ | sha1sum

and not

echo “abc123″ | sha1sum

Without the -n echo adds a new line to the text. Like if you were to type abc123 then hit enter.

Notice :

echo “abc123″ | sha1sum
61ee8b5601a84d5154387578466c8998848ba089

echo -n “abc123″ | sha1sum
6367c48dd193d56ea7b0baad25b19455e529f5ee

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Digg
  • StumbleUpon
  • Technorati
  • Netscape
  • Fark
  • Slashdot