Some useful bash shell scripts for the Linux user.

This function will allow your computer to speak.

function shellspeak()
{
       mplayer "http://translate.google.com/translate_tts?tl=en&q=$(echo $@ | sed 's/\s/+/')" > /dev/null 2>&1;
}

A script that will change all files in a directory to lowercase filenames.

function lowercase()  # move filenames to lowercase.
{
    for file ; do
        filename=${file##*/}
        case "$filename" in
        */*) dirname==${file%/*} ;;
        *) dirname=.;;
        esac
        nf=$(echo $filename | tr A-Z a-z)
        newname="${dirname}/${nf}"
        if [ "$nf" != "$filename" ]; then
            mv "$file" "$newname"
            echo "lowercase: $file --> $newname"
        else
            echo "lowercase: $file not changed."
        fi
    done
}

A function that will check for all invalid symlinks in a directory.

function badlink()
# From Atomic magazine #43 August 2004. http://www.atomicmpc.com.au
{
	DEFAULT=$(tput sgr0);
	FILELIST=.badlink.list

	[ -e $FILELIST ] && $( rm -fr $FILELIST )

	function checklink()
	{
		for badlink in $1/*; do
			[ -h "$badlink" -a ! -e "$badlink" ] && echo \
			\"$badlink\" >> $FILELIST
			[ -d "$badlink" ] && checklink $badlink
		done
	}

	for directory in `pwd`; do
		if [ -d $directory ] ; then
			checklink $directory;
		fi
	done

	if [ -e $FILELIST ] ; then
		for line in $(cat $FILELIST); do
			echo $line | xargs -r rm | echo -e "$line \
			-removed"
			echo
		done
		rm -fr $FILELIST
	else
		printf "Bad symlinks not found.\n\n"
	fi
} # End Atomic function.

Get your network IP addresses easily with this simple function.

function my_ip() # get IP adresses. Bracket on next line C style...
{
    MY_IP=$(/sbin/ifconfig wlan0 | awk '/inet/ { print $2 } ' | sed -e s/addr://)
    MY_ISP=$(/sbin/ifconfig wlan0 | awk '/P-t-P/ { print $3 } ' | sed -e s/P-t-P://)
}

Get user information from the shell.

if [ -x /usr/bin/finger ] ; then
   INFO=$(finger -lmps $LOGNAME | sed -e "s/On/Logged in/g" | grep "since" )
else
   INFO=$(uname -msov)
fi

echo -ne "${INFO}\n"

No comments have been made. Use this form to start the conversation :)

Leave a Reply