Writers' Community!
Home Page Two Columnists Q&A Submit an Article FAQs Contact Author Login
Article Submission
We Need YOUR Articles!
We'll Promote Them for FREE!

Author Login

New Authors
Register Here


Now Serving 7,775 Authors
70,467 Quality Articles
& 7,557 Current Users Online!
Featured Authors
Bruce Horst (142)
Joel Hendon (16,285)
Michael Ramzy (633)
E. Raymond Rock (3,068)
Ira Coffin (6,669)
Connor Davidson (5,131)
Ben Morrish (7,936)
Steve Kovacs (4,545)
Sandra E. Graham (7,883)
Fran Larson (2,271)
Shari Vaudo (418)
David Tanguay (9,577)
Missing Link (766)
Gregory Lewis (1,603)

View All Featured Authors
Most Recent
Basic Computer Science: Key Name for your computer keyboard

How to Connect to a Wireless Network in Windows Vista

Creating an Email Signature in Microsoft Outlook

How to Disable User Account Control in Windows Vista

Fixing A Windows XP Slow Boot

Four Tips to Protect Your PC from Viruses and Intruders

What if I forget my login password in Windows XP?

How I Obtained A Free Computer System

Mozilla Firefox Beats Internet Explorer

Windows Vista Registry Scan - How It Works

Home » Categories » Computers & Networking » Operating Systems » Example Unix Shell Scripts » Printer Friendly

Example Unix Shell Scripts

Rated 3 out of 5
No Reader Ratings Available ?
Rate It  /  View Comments  /  View All Articles submitted by John Roberts
Submitted Saturday, December 31, 2005
John Roberts (341)
JayrConsulting Ltd
Log in to become a member of John Roberts's Fan Club!


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









Reprint Rights

Log in to become a member of John Roberts's Fan Club!

Comments on this article: (1 total)


» left by Anonymous (3 years 129 days ago.)
Reader Rating: 5 out of 5
some useful little gems here, i learned some things , thanks
Respond to this comment

Was this article helpful to you? Leave a Public Comment or Question:

This Article has been viewed 2,636 times.
Article added to SearchWarp.com on 12/31/2005 11:52:01 AM.
View other articles written by John Roberts (341)


If you found this article interesting, you may want to check out:

Disclaimer:  All information on this site is provided for informational purposes only! By no means is any information presented herein intended to substitute for the advice provided to you by any health care or other professional or organization.


Today's Most Popular
Five Steps to Make Windows XP Shut-Down Faster

How to Speed Up your Network Performance in Windows XP - For Real

Windows Run Commands List

Slow Internet Explorer

Partitioning, Formatting and Reinstalling in Windows 98

Example Unix Shell Scripts

Ten Steps To Speed Up Your Windows XP Startup

How I got rid of Spyware Guard 2008

Troubleshooting Windows XP When Not Booting

What To Do When Windows Won't Boot

Viewed from Cache. Load Time: 0.016.

Home  |  Page Two  |  FAQ's  |  Contact  |  Terms of Service  |  Article Submission Guidelines  |  Questions & Answers  |  Privacy  |  Mission / About
Copyright © 1999-2009 SearchWarp.com, All Rights Reserved - SearchWarp.com is an IcoLogic, Inc. Company