From Bob Brandt's Projects Site

Linux: Useful Linux Scripts and Commands

Great SED Reference Site

Display a Hex dump of a file

od -Ax -tx1z -v /path/to/filename

Trim Strings

# Front Trim
echo "   the test   " | sed 's/^[[:space:]]*//'

# Rear Trim
echo "   the test   " | sed 's/[[:space:]]*$//'

# Full Trim (the -e tells sed that there is more than one command)
echo "   the test   " | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'

Find out how many specific characters there are in a string (i.e. how many t's are in a test string)

char=t
numchar=`echo " the test string " | sed 's/[^$char]//g'`
numchar=${#numchar}

Search and replace text within a file

filename="$1"
searchtext="$2"
replacetext="$3"

sed 's~'"$searchtext"'~'"$replacetext"'~g' $filename

Make sure user is root

if [ "`id -u`" != "0" ]; then
	echo "You must be root to run this script" && exit 1
fi

Ask for user input

##############################################################################
# Below is the code responsible for producing asking a question              #
#                                                                            #
# USAGE:  userinput Question DefaultAnswer                                   #
#                                                                            #
##############################################################################
function userinput {
	echo -e -n "$1 [$2]: "
	read value
	if [ -z "$value" ]; then
		value=$2
	fi
	echo
}

Script to find all the MAC Addresses Of Windows PCs in an IP subnet.

#!/bin/bash

if [ "$1" == "" ]; then
	echo -e "Used to create a CSV file of all the IP Addresses in the a subnet and the MAC\n addresses of those Windows IPs" 2>&1
	echo -e "Usage: $0 aaa.bbb.ccc\n" 2>&1
	exit 1
fi

for i in `seq 1 254`
do

	ip="$1.$i"
	if ping -c 1 $ip > /dev/null
	then
		result=`nmblookup -A $ip | grep "MAC Address = "`
		if [ "$result" != "" ]; then
			echo "\"$ip\", \"$result\""
		fi
	fi
done
exit 0

Find a full path from a relative path

function findpath() {
	relativepath=`echo "$0" | sed -n 's|\(.*\)/.*|\1|p'`
	pushd "$relativepath" > /dev/null ; pwd ; popd > /dev/null
}

Find the name of a script file

function findname() { echo $0 | sed 's|.*/||' }

Find the fully qualified host name of a system (i.e. host.domain.name)

function fullhostname {
domain=sed -n 's|^search\s\(.*\)|\1|p' /etc/resolv.conf | sed 's|\s.*||' | sed -n 's|\w\+\.\w\+|&|p'
if [ -n "$domain" ]; then
	domain=".$domain"
fi
echo `hostname`"$domain"
}

Monitor the /home drive and report if its usage is greater than 80%.

#!/bin/bash

declare -i usage=0

usage=`df | grep "/home" | cut -c 52-54`
if [ $usage -gt 4 ]; then
	du --max-depth=1 /home | mail -s "Warning: Usage of /home is above 80 percent" "brandtb" 
fi
exit 0

How to Create an ISO File in Linux

dd if=/dev/cdrom of=/cdrom_image.iso
Retrieved from /projects/pmwiki.php?n=Linux.MiniScripts
Page last modified on August 14, 2009, at 01:05 PM