Example Shell Scripts ( Addendum to Unix Fundamentals from JayrConsulting Ltd. )
#Space prog. Converts df command output into a more user friendly #format
#
tput clear
bof=`tput rmso`
bon=`tput smso`
echo "\n"
echo "\t\t\t $bon Disk Space Summary $bof "
echo "\n"
for FS in `df -P|tail +2|grep -v proc|grep -v fd|awk ' { print $6 } ' `
do
CK=`df -P|grep "$FS" |awk ' { print $4 } '|head -1 `
MB=`bc -l << stop
$CK / 2048
stop`
MB=`echo $MB|cut -c0-6`
echo "The $bon $FS $bof File System has $bon $MB $bof Mbytes of space left "
echo ""
done
*****************************next prog*******************
#staff-enter prog. Shows simple use of shell commands and screen #handling
#using 'tput' to produce an input screen and a simple database file, which
#could then be interrogated using grep or awk etc.
#
#
while true
do
tput clear
echo "\t\t\t `tput smso` STAFF INPUT SCREEN `tput rmso` "
tput cup 3 10
echo "First name : "
tput cup 3 40
echo "Surname : "
tput cup 7 10
echo " Job Title : "
tput cup 7 40
echo "Location : "
tput cup 12 10
echo "Type of car: "
tput cup 12 40
echo "Salary : "
tput cup 15 10
echo "ID Number : "
tput cup 3 27
read CN
tput cup 3 52
read SN
tput cup 7 24
read JT
tput cup 7 55
read OL
tput cup 12 24 read TC
tput cup 12 54
read SY
tput cup 15 25
read PP
echo "\n\n"
echo "$PP:$CN:$SN:$JT:$OL:$TC:$SY"|tr 'a-z' 'A-Z' >> ./peoplesort -n -o ./peope ./people
echo "Another Entry (y or n) : \c"
read ANS
case $ANS in
y|Y) continue
n|N) exit
*) exit
esac
done
*****************************next prog*******************
# time loop program . shows basic function of 'while' command by decrementing
# a variable and keeping the loop operating until the value of the variable
# produces a false response to the 'while statement.
#
#
num=10
while [ $num -gt 4 ]
do
banner $num
sleep 1
num=`expr $num - 1`
done
*****************************next prog*******************
#simple loop program to show how 'for' checks along the item list on each
#invocation of the loop.
#
#
for spam in a 3 f 5 h y 6
do
echo "Next run through the loop program "
echo ""
echo " Now I am looking at the $spam item in the list "
sleep 2
done
*****************************next prog*******************
****************************next prog******************* # second 'for' loop program to show how the item list can be filled from the
# output of another command.
#
#
for a in `who|cut -c0-12`
do
echo " The user $a is logged on "
echo ""
sleep 1
done
****************************next prog*******************
# script to allow a remote terminal to be logged off in a clean and #orderly
# fashion making sure that processes are shut down in the right order #and
# are killed with a signal 15 where possible.
#
#
while true
do
who -u
echo " Enter Terminal to be logged off : \c"
read POP
if [ "$POP" = "" ]
then
exit
fi
for a in `ps -t $POP|tail +2|awk ' { print $1 } ' |sort -n -r`
do
kill -15 $a
kill -9 $a 2> /dev/null
done
echo "Please wait ! Clearing processes from Terminal $POP........."
sleep 2
who -u
echo "Do you want to log off another user : y/n ? : \c "
read ANS
case ANS in
y|Y) continue
*) exit
esac
done
****************************next prog*******************
****************************next prog*******************
# simple script to show how menu programs can be constructed using 'while loops
# and 'case' statements. Note use of 'trap' on signals which prevents breaking
# out from the program loop other than by an allowed exit.
#
#
trap "echo ILLEGAL ENTRY " 2 3 15
while true
do
tput clear
echo "\t\t\t MENU PROGRAM "
echo "\n\n"
echo "\t\t\t Check Users 1 "
echo "\t\t\t Check Disk Space 2 "
echo "\t\t\t Exit from Menu 3 "
echo "\n\n"
echo "\t\t\t Enter Option Number :\c"
read PIP
case $PIP in
1) echo "\t\t LIST OF LOGGED ON USERS "
who -u
echo "\n"
echo "\t Press Enter to continue : \c "
read ANS
2) df -k
echo "\n"
echo "\t Press Enter to continue : \c "
read ANS
3) exit
*) echo " You MUST enter option 1 or 2 or 3 "
echo "Press Enter to continue : \c "
read ANS
esac
done
****************************next prog*******************
# script which can be used to automatically edit a file and remove any #blank
# lines that it finds. Filename need to be entered on command line #after the
# command itself.
#
ed -s $1 << stop > /dev/null
g/^$/d
w
Q
stop
****************************next prog*******************
# datacheck program. checks against data in the file 'data' by using #the command
# datacheck < data . Looks for records that start with '03' and have #a '9' in
# the eighth bit. It then discards this line and any following lines up #until the
# next line that starts '03' and eighth bit NOT '9'. Amended data #records
# are written to a file called datanew and a historical record is kept #in the
# file datalog. Discarded data can be viewed by running the command
# diff data datanew which will show differences between the #files.
#
#
> ./datanew
FILEOK=1
DISCARD=0
while read PASDATA
do
tput clear
tput cup 10 20
echo " Checking data records..............\c"
if echo $PASDATA |grep "^03.*" > /dev/null
then
if echo $PASDATA |grep ".......9.*" > /dev/null
then
DISCARD=1
FILEOK=0
else
DISCARD=0
fi
fi
if [ $DISCARD = "0" ]
then
echo $PASDATA >> ./datanew
fi
done
if [ FILEOK = "1" ]
then
echo " `date` File O.K. " >> ./datalog
else
echo " `date` Error in file---Has been cleared " >> ./datalog
fi
tput clear
echo " Data Check Complete................"
****************************next prog*******************
****************************next prog*******************
# show program, execute with 'show file-name'. Counts the number of #lines in
# the file with wc -l and then decides to either 'cat' or 'pg' the #file,
# depending on its length.Note '-p' option with pg allows insertion of #yourown text at the ':' prompt.
#
#
if `wc -l $1|cut -c0-8` -lt 20
then
cat $1
else
pg -p "Return to continue or'h' for help " $1
fi