kill all processes for a user

usage: killuser username

#!/bin/bash
USER=$1
MYNAME=`basename $0`
if [ ! -n "$USER" ]
then
echo "Usage: $MYNAME username" >&2
exit 1
elif ! grep "^$USER:" /etc/passwd >/dev/null
then
echo "User $USER does not exist!" >&2
exit 2
fi
while [ `ps -ef | grep "^$USER" | wc -l` -gt 0 ]
do
PIDS=`ps -ef | grep "^$USER" | awk ‘{print $2}’`
echo "Killing " `echo $PIDS | wc -w` " processes for user $USER."
for PID in $PIDS
do
kill -9 $PID 2>&1 >/dev/null
done
done
echo "User $USER has 0 processes still running."

display an X window message when it’s time to take a break

script name: xbreak
purpose: display an X window message when it’s time to take a break
requires: standard GNU commands
version: 1.0
usage: xbreak [-hl]
options:

, an integer in minutes; for every n minutes a break message is displayed
-h, usage and options (this help)
-l, see this script"

#! /bin/sh
# #############################################################################

NAME_="xbreak"
HTML_="remind to take a break"
PURPOSE_="display an X window message when it’s time to take a break"
SYNOPSIS_="$NAME_ [-hl] "
REQUIRES_="standard GNU commands"
VERSION_="1.0"
DATE_="2004-06-23; last update: 2004-07-01"
AUTHOR_="Dawid Michalczyk "
URL_="www.comp.eonworks.com"
CATEGORY_="desk"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ – $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
, an integer in minutes; for every n minutes a break message is displayed
-h, usage and options (this help)
-l, see this script"
exit 1
}

# enabling extended globbing
shopt -s extglob

# option handling
case $1 in
-h) usage ;;
-l) more $0; exit 1 ;;
+([0-9])) # arg1 can only be an integer

while :;do

sleep ${1}m
# display windowed message if x is running
ps -aux | grep -q xinit
[ $? = 0 ] && xmessage -center Time for a break!

done ;;

*) echo invalid argument, type $NAME_ -h for help; exit 1 ;;

esac

display an X window message on a regular basis

script name: xmesg
purpose: display an X window message on a regular basis
requires: standard GNU commands
version: 1.0
usage: xmesg [-hl] s|m|h|d ‘
options:

s|m|h|d, an integer referring to seconds|minutes|hours|days; the time
interval for how often to display the message
‘, a single quoted message to display
-h, usage and options (this help)
-l, see this script"

#! /bin/sh
# #############################################################################

NAME_="xmesg"
HTML_="show specific X window message"
PURPOSE_="display an X window message on a regular basis"
SYNOPSIS_="$NAME_ [-hl] s|m|h|d ‘‘"
REQUIRES_="standard GNU commands"
VERSION_="1.0"
DATE_="2004-03-14; last update: 2004-03-14"
AUTHOR_="Dawid Michalczyk "
URL_="www.comp.eonworks.com"
CATEGORY_="desk"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ – $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
s|m|h|d, an integer referring to seconds|minutes|hours|days; the time
interval for how often to display the message
‘, a single quoted message to display
-h, usage and options (this help)
-l, see this script"
exit 1
}

# enabling extended globbing
shopt -s extglob

# option handling
case $1 in
-h) usage ;;
-l) more $0; exit 1 ;;
+([0-9])[smhd] )

[[ $2 ]] || { echo >&2 missing second argument, type $NAME_ -h for help; exit 1; }

while :;do

sleep ${1}
# display windowed message if x is running
ps -aux | grep -q xinit
[ $? = 0 ] && xmessage -center $2

done ;;

*) echo invalid or missing argument, type $NAME_ -h for help; exit 1 ;;

esac

unpack zip, tar, tgz, tar.gz, tar.bz2, tar.z

script name: unpack2dir
purpose: unpack zip, tar, tgz, tar.gz, tar.bz2, tar.z to a dir of the same name as archive prefix
requires: standard GNU commands
version: 1.2
usage: unpack2dir [-vhlr] [file…]
options:

-r, remove the compressed file after extraction
-v, verbose
-h, usage and options (help)
-l, see this script"

#! /bin/sh
# #############################################################################

NAME_="unpack2dir"
HTML_="uncompress unpack script"
PURPOSE_="unpack zip, tar, tgz, tar.gz, tar.bz2, tar.z to a dir of the same name as archive prefix"
SYNOPSIS_="$NAME_ [-vhlr] [file…]"
REQUIRES_="standard GNU commands"
VERSION_="1.2"
DATE_="1999-09-20; last update: 2006-02-03"
AUTHOR_="Dawid Michalczyk "
URL_="www.comp.eonworks.com"
CATEGORY_="compress"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

# HISTORY:
# 2006-02-03 v1.2 – added the -C flag to tar options. Otherwise tar would not
# extract to a dir with different name then the one found in the
# archive.

usage () {

echo >&2 "$NAME_ $VERSION_ – $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
-r, remove the compressed file after extraction
-v, verbose
-h, usage and options (help)
-l, see this script"
exit 1
}

# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# var init
rmf=
verbose=

# option and argument handling
while getopts vhlr options; do

case $options in
r) rmf=on ;;
v) verbose=on ;;
h) usage ;;
l) more $0; exit 1 ;;
\?) echo invalid argument, type $NAME_ -h for help; exit 1 ;;
esac

done
shift $(( $OPTIND – 1 ))

mkdirf() {

# usage: fnc

[ -d $1 ] && { echo "${NAME_}: skipping ${2} – dir ${1} already exist" ; continue; }
#echo $1
mkdir $1
# [[ $verbose ]] && echo "${NAME_}: unpacking "$2
}

file_getDirname() {

local _dir="${1%${1##*/}}"
[ "${_dir:=./}" != "/" ] && _dir="${_dir%?}"
echo "$_dir"

}

file_getBasename() {

local _name="${1##*/}"
echo "${_name%$2}"

}

clean() {

# usage

[[ $1 != 0 ]] && rmdir $2 # remove empty dir if unpacking went wrong
[[ $1 == 0 && $verbose ]] && echo "${NAME_}: unpacking " ${dir}/${a}
[[ $rmf ]] && rm -f — $a

}

start_dir=$(pwd)

for a in "$@"; do

cd $start_dir
fname=$(file_getBasename $a)
dir=$(file_getDirname $a)
cd $dir
a=$fname

case $a in

# zip
*.[zZ][iI][pP])
mkdirf ${a/.[zZ][iI][pP]/} $a
unzip -qq $a -d ${a/.[zZ][iI][pP]/}
clean $? ${a/.[zZ][iI][pP]/}
;;

# tar
*.[tT][aA][rR])
mkdirf ${a/.[tT][aA][rR]/} $a
tar -xf $a -C ${a/.[tT][aA][rR]/}/
clean $? ${a/.[tT][aA][rR]/}
;;

# tgz
*.[tT][gG][zZ])
mkdirf ${a/.[tT][gG][zZ]/} $a
tar -xzf $a -C ${a/.[tT][gG][zZ]/}
clean $? ${a/.[tT][gG][zZ]/}
;;

# tar.gz
*.[tT][aA][rR].[gG][zZ])
mkdirf ${a/.[tT][aA][rR].[gG][zZ]/} $a
tar -xzf $a -C ${a/.[tT][aA][rR].[gG][zZ]/}/
clean $? ${a/.[tT][aA][rR].[gG][zZ]/}
;;

# tar.bz2
*.[tT][aA][rR].[bB][zZ]2)
mkdirf ${a/.[tT][aA][rR].[bB][zZ]2/} $a
tar -xjf $a -C ${a/.[tT][aA][rR].[bB][zZ]2/}/
clean $? ${a/.[tT][aA][rR].[bB][zZ]2/}
;;

# tar.z
*.[tT][aA][rR].[zZ])
mkdirf ${a/.[tT][aA][rR].[zZ]/} $a
tar -xZf $a -C ${a/.[tT][aA][rR].[zZ]/}/
clean $? ${a/.[tT][aA][rR].[zZ]/}
;;

*) echo "${NAME_}: $a not a compressed file or lacks proper suffix" ;;

esac

done

Update your Root DNS hint zone

Update your Root DNS hint zone file with this script.

#!/bin/sh
#
# Update the nameserver cache information file once per month.
# This is run automatically by a cron entry.
#
# Original by Al Longyear
# Updated for BIND 8 by Nicolai Langfeldt
# Updated for BIND 9 on RedHat Systems by Souvik Prasad
# Miscelanious error-conditions reported by David A. Ranch
# Ping test suggested by Martin Foster
# named up-test suggested by Erik Bryer.
#
(
echo "To: root@localhost"
echo "From: root@localhost"

# Is named up? Check the status of named.
case `rndc status 2>&1` in
*refused*)
echo "named is DOWN. named.ca was NOT updated"
echo
exit 0
;;
esac

PATH=/sbin:/usr/sbin:/bin:/usr/bin:
export PATH
# NOTE: /var/named must be writable only by trusted users or this script
# will cause root compromise/denial of service opportunities.
cd /var/named 2>/dev/null || {
echo "Subject: Cannot cd to /var/named, error $?"
echo
echo "The subject says it all"
exit 1
}

# Are we online? Ping a server at your ISP
case `ping -qnc 1 -w60 www.vsnl.com 2>&1` in
*’100% loss’*)
echo "Subject: named.ca NOT updated. The network is DOWN."
echo
echo "The subject says it all"
exit 1
;;
esac

dig @e.root-servers.net . ns > named.ca.new 2> errors

case `cat named.ca.new` in
*NOERROR*)
# It worked
:;;
*)
echo "Subject: The named.ca file update has FAILED."
echo
echo "The named.ca update has failed"
echo "This is the dig output reported:"
echo
cat named.ca.new errors
exit 1
;;
esac

echo "Subject: The named.ca file has been updated"
echo
echo "The named.ca file has been updated to contain the following
information:"
echo
cat named.ca.new

chown root.root named.ca.new
chmod 444 named.ca.new
rm -f named.ca.old errors
mv named.ca named.ca.old
mv named.ca.new named.ca
rndc reload 2>&1
echo
echo "The nameserver has been restarted to ensure that the update is complete."
echo "The previous named.ca file is now called
/var/named/named.ca.old."
) 2>&1 > /tmp/update-named.ca
cat /tmp/update-named.ca | /usr/sbin/sendmail -t
rm -f /tmp/update-named.ca
exit 0

Script to run a Message Board on the web server

Script to run a Message Board on the web server.
Users write messages in their own $HOME/mboard file, and they are chronologically (upto last 10 days and maximum of 4 lines of 80chars each for each user) arranged on the webpage mboard.html of the webserver.

#!/bin/bash
#####################################################################################
### Program to copy User Messages into Message Board; Souvik Prasad (27.01.03.) ###
#####################################################################################

#Message file name in user home directory
MFILE=mboard
# Temporary Directory
MBDIR="/tmp/mboard"
# Headline color
HCOLOR="\"#000055\""
# Document background color
BGCOLOR="\"#FDFDFD\""
# Messenger’s Name color
NCOLOR="\"#EE3300\""
# Message header background color
MHBGCOLOR="\"#FFEEEE\""
# The message background color
MBGCOLOR="\"#CCCCCC\""
# Refresh Interval in seconds
REFRESHSEC=60
# Maximum number of admissible lines per message
MAXLINES=6

prwd=`pwd`
rm -Rf $MBDIR
mkdir -p $MBDIR
cd $MBDIR
for fname in $(find /home -maxdepth 2 -type f -name $MFILE -mtime -10 -not -empty);
do
dest=`echo $fname| cut -d/ -f3`
cp -pf $fname $dest 2> /dev/null
if [ $(file -b $dest| grep -ce "^ASCII") != 1 ] ; then
rm -f $dest
fi
done
filesall=`/bin/ls -t`
echo -e "" >> html.html
echo -e "" >> html.html
echo -e "" >> html.html
echo -e " " >> html.html
echo -e " " >> html.html
echo -e " " >> html.html
echo -e " Message Board" >> html.html
echo -e "" >> html.html
echo -e "" >> html.html
echo -e "

\n

Message Board

" >> html.html
echo -e "Last Updated on : `date`\n

\n
 " >> html.html
echo -e "


\n
" >> html.html
if [ -z "$filesall" ] ; then
echo -e "
 
 
 " >> html.html
echo -e "

\n\nNo New Message In Last 10 Days." >> html.html
echo -e "
\n

\n
 
 
 " >> html.html
else
echo -e "

" >> html.html
for fname in $filesall ;
do
echo -e "

\n

\n

\n

\n

\n

\n

" >> html.html
done
echo -e "

\n" >> html.html
echo -n `awk -F: "\\\$1 == \"$fname\" { print \\\$5 }" /etc/passwd | cut -d, -f 1` >>html.html
echo -en "
\n
(" >> html.html
echo -e "$fname@juphys.ernet.in
)" >> html.html
echo -e "
" >> html.html
echo `/bin/ls -l $fname | cut -b 43-55` >> html.html
echo -e "
" >> html.html
head -$MAXLINES $fname | cut -b -80 | awk ‘ ORS="
" { print } ‘ >> html.html
echo -e "

" >> html.html
fi
echo -e "

\n


\n" >> html.html
echo -e "

Message Board © 2003 Souvik Prasad.

" >> html.html
echo -e "\n" >> html.html
cp -f html.html /var/www/html/mboard.html
cd $prwd
rm -Rf $MBDIR

convert ps file to ascii text

#! /bin/sh
# #############################################################################

NAME_="ps2txt"
HTML_="ps to text"
PURPOSE_="convert ps file to ascii text; write the converted file to disk"
SYNOPSIS_="$NAME_ [-vhlr] [file…]"
REQUIRES_="standard GNU commands, ps2ascii"
VERSION_="1.0"
DATE_="2004-04-18; last update: 2005-03-03"
AUTHOR_="Dawid Michalczyk "
URL_="www.comp.eonworks.com"
CATEGORY_="text"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ – $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
-r, remove input file after conversion
-v, verbose
-h, usage and options (help)
-l, see this script"
exit 1
}

# arg check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# var initializing
rmf=
verbose=

# option and argument handling
while getopts vhlr options; do

case $options in
r) rmf=on ;;
v) verbose=on ;;
h) usage ;;
l) more $0 ;;
\?) echo invalid or missing argument, type $NAME_ -h for help; exit 1 ;;
esac

done

shift $(( $OPTIND – 1 ))

# check if required command is in $PATH variable
which ps2ascii &> /dev/null
[[ $? != 0 ]] && { echo >&2 the required \"ps2ascii\" command is not in your PATH; exit 1; }

# main
for a in *.[pP][sS]; do

if [ -f ${a%.*}.txt ]; then
echo ${NAME_}: skipping: ${a%.*}.txt file already exist
continue
else
[[ $verbose ]] && echo "${NAME_}: converting: $a -> ${a%.*}.txt"
ps2ascii $a > ${a%.*}.txt
[[ $? == 0 ]] && stat=0 || stat=1
[[ $stat == 0 ]] && [[ $verbose ]] && [[ $rmf ]] && echo ${NAME_}: removing: $a
[[ $stat == 0 ]] && [[ $rmf ]] && rm -f — $a
fi

done

lowspace

#! /bin/sh
# #############################################################################

NAME_="lowspace"
HTML_="monitor space on device"
PURPOSE_="notify when space on a device is less then n Megabytes"
SYNOPSIS_="$NAME_ [-hlx] -i -d -t s|m|h|d"
REQUIRES_="standard GNU commands, xmessage"
VERSION_="1.5"
DATE_="1999-08-10; last update: 2005-02-28"
AUTHOR_="Dawid Michalczyk "
URL_="www.comp.eonworks.com"
CATEGORY_="admin"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ – $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
-i , an integer referring to Mb
-d , full path to the device eg: /dev/hda1
-t s|m|h|d, n is an integer referring to time interval in
seconds|minutes|hours|days which refers to the frequency of how
often to check for low space on the device
-x, use xmessage to display a windowed notification if running X
-h, usage and options (this help)
-l, see this script"
exit 1
}

shopt -s extglob # enabling extended globbing

time_validateSleepArg() {

case $1 in
+([0-9])[smhd] ) return 0 ;;
*) return 1 ;;
esac
}

# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# var init
xmsg=

while getopts hlxd:i:t: options; do

case $options in
i) mb=$OPTARG ;;
d) device=$OPTARG ;;
x) xmsg=on ;;
t) time=$OPTARG ;;
h) usage ;;
l) more $0; exit 1 ;;
\?) echo invalid argument, type $NAME_ -h for help; exit 1 ;;

esac

done
shift $(( $OPTIND – 1 ))

# check if required command is in $PATH variable
if [ $xmsg ]; then
which xmessage &> /dev/null
[[ $? != 0 ]] && { echo >&2 the required \"xmessage\" command is not in your PATH; exit 1; }
fi

# args check
[[ $device ]] || { echo >&2 the -d option and argument must be specified; exit 1; }
[[ $time ]] || { echo >&2 the -t option and argument must be specified; exit 1; }
[[ $mb == *[!0-9]* ]] && { echo >&2 the argument to option -i must be an integer; exit 1; }

# sleep arg check
time_validateSleepArg $time
[ $? = 0 ] || { echo >&2 invalid argument to option -t, type $NAME_ -h for help; exit 1; }

# main
while :;do

free=$(df -m | grep $device | { read dev blocks used avail perc mounted ; echo $avail; })
[ $free ] || { echo >&2 wrong device name, type $NAME_ -h for help; exit 1; }

if (( $free < $mb ));then if [ $xmsg ];then # display windowed message if x is running; ring a bell ps -aux | grep -q xinit if [ $? = 0 ];then echo -en \\a xmessage -center Device $device is running low on space! Only ${free}Mb left. exit else echo -e \\a Device $device is running low on space! Only ${free}Mb left. exit fi else # write message to terminal and ring a bell echo -e \\a Device $device is running low on space! Only ${free}Mb left. exit fi fi sleep $time # how often to check devices done

extract email addresses from file or stdin

#! /bin/sh
# #############################################################################

NAME_="emailadext"
HTML_="isolate email address"
PURPOSE_="extract email addresses from file or stdin"
SYNOPSIS_="$NAME_ [-hl] [file…]"
REQUIRES_="standard GNU commands"
VERSION_="1.0"
DATE_="2004-06-24; last update: 2004-06-25"
AUTHOR_="Dawid Michalczyk "
URL_="www.comp.eonworks.com"
CATEGORY_="www"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ – $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
-h, usage and options (this help)
-l, see this script"
exit 1
}

# options
case $1 in
-h) usage ;;
-l) more $0 ; exit 1 ;;
esac

# main
cat "$@" | { # so we can act as a filter
tr ‘,;<>()"\47 ‘ ‘[\n*]’ | sed -n -e ‘s/mailto://gI’ -e ‘/@/p’
}

rename files

script name: neatname
purpose: neatly rename files
requires: standard GNU commands
version: 1.3
usage: neatname [-vhl] -p -i -s [file…]
options:

-p, prefix to use on file names
-i, integer number to start with
-s, file suffix to use on file names
-v, verbose
-h, usage and options (this help)
-l, see this script"

#! /bin/sh
# #############################################################################

NAME_="neatname"
HTML_="rename files script"
PURPOSE_="neatly rename files"
SYNOPSIS_="$NAME_ [-vhl] -p -i -s [file…]"
REQUIRES_="standard GNU commands"
VERSION_="1.3"
DATE_="1998-10-12; last update: 2005-06-15"
AUTHOR_="Dawid Michalczyk "
URL_="www.comp.eonworks.com"
CATEGORY_="file"
PLATFORM_="Linux"
SHELL_="bash"
DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ – $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
-p, prefix to use on file names
-i, integer number to start with
-s, file suffix to use on file names
-v, verbose
-h, usage and options (this help)
-l, see this script"
exit 1
}

# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# var init
verbose=
prefix=
suffix=

# option and argument handling
while getopts vhlp:i:s: options; do

case "$options" in
p) prefix="$OPTARG" ;;
i) int="$OPTARG" ;;
s) suffix="$OPTARG" ;;
v) verbose=on ;;
h) usage ;;
l) more $0; exit 1 ;;
\?) echo invalid argument, type $NAME_ -h for help; exit 1 ;;
esac

done
shift $(( $OPTIND – 1 ))

# int check
[[ $int == *[!0-9]* ]] && { echo >&2 the argument to option -i must be an integer; exit 1; }

# args check: any files to work on?
[[ $@ ]] || { echo >&2 no files specified, type $NAME_ -h for help; exit 1; }

# main execution
for a in "$@"; do

newf="${prefix}"${int}"${suffix}"
if [ -f "$newf" ]; then
echo "${NAME_}: skipping renaming $a – $newf already exist" && continue
else
[ -f "$a" ] && { mv "$a" "$newf"; } || continue
[[ $verbose ]] && echo "${NAME_}: $a -> $newf"
fi
((int++))

done