Shell Docs
Shell Docs
CHAPTER 1
######################################################################
CHAPTER 2
##########
# counts #
##########
#
# First, all possible users. These are found in the file "/etc/passwd"
#
echo "Number of possible users on the system: \c"
cat /etc/passwd | wc -l
#
# Next, all users currently logged in (using "who")
#
echo "Number of users logged onto the system: \c"
who | wc -l
#
# Finally, the number of running processes (using "ps -e")
#
echo "Total number of processes running: \c"
ps -e | wc -l
###############
# readdetails #
###############
#
# First, the user's name
#
echo "Please enter your name: \c"
read name
echo Name: $name > details.out
#
# Next, the user's address
#
echo "Please enter your address: \c"
read address
echo Address: $address >> details.out # note: APPENDING!
#
# Next, the user's phone number
#
echo "Please enter your phone number: \c"
read phone
echo Phone: $phone >> details.out
#
# Finally, display the message about the filename
#
echo Thank you. The details have been stored in the file \"details.out\"
######################################################################
CHAPTER 3
######################################################################
CHAPTER 4
##############
# age_script #
##############
echo $age
##############
# joe_script #
##############
myname=Joe
echo "Hello $myname, today's date
is `date +%d/%m/%y`"
###############
# more_script #
###############
#############
# variables #
#############
name1=Fred
echo $name1
name2='Fred Smith'
echo $name2
name3="That's Life"
echo $name3
######################################################################
CHAPTER 5
###########
# agetest #
###########
retirement_age=65
echo "Please enter your age: \c"
read age
if [ "$age" = "" ]
then
echo You did not enter an age
elif [ $age -gt $retirement_age ]
then
echo You should be retired
else
echo You are still quite young
fi
###########
# dirtest #
###########
if [ "$dname" = "" ]
then
echo You did not enter a directory
elif [ -d $dname -a -w $dname ]
then
echo Well done! > $dname/hello
else
echo $dname is not a writable directory - nothing written
fi
######################################################################
CHAPTER 6
###########
# fooloop #
###########
pattern=foo
total=0
for f in *
do
[ ! -f $f ] && continue
#############
# formatwho #
#############
who | sort | awk '{printf("%-15s%s %2s %s\n", $1, $3, $4, $5)}'
##############
# grepscript #
##############
file=greptest
echo lines that have the pattern [x,y] in them, where x and y are any numbers:
grep -E '\[[0-9]+,[0-9]+\]' $file
echo ===============================
############
# greptest #
############
vector [1,4]
This is line 10
This is line 11
vector2 [x,y]
vector3 [,345] this line should not be seen in the last solution
vector4 [14,55]
###############
# parsepasswd #
###############
####################
# parsepasswd2.awk #
####################
#!/bin/awk -f
BEGIN {
FS = ":";
printf("Username Directory\n");
printf("================================\n");
}
{
printf("%-12s%-20s\n", $1, $6)
}
#############
# sedscript #
#############
#!/bin/sed -f
#
# This script will be run using the sed program
#
/gumby/d
############
# contacts #
############
#!/bin/sh
# The above line causes this script to be run using the Bourne Shell (sh)
#######################################################################
#
# Script to maintain a contacts database.
#
#######################################################################
#
# Define the name of the file
#
fname=names.dat
#
# If the file does not exist, create it
#
[ ! -f $fname ] && > $fname
#
# Loop forever - until they want to exit the program
#
while true
do
#
# Display the menu
#
clear
echo "\n\t\tSHELL PROGRAMMING DATABASE"
echo "\t\t\tMAIN MENU"
echo "\nWhat do you wish to do?\n"
echo "\t1. Create records"
echo "\t2. View records"
echo "\t3. Search for records"
echo "\t4. Delete records that match a pattern"
echo
#
# Prompt for an answer
#
echo "Answer (or 'q' to quit)? \c"
read ans junk
#
# Empty answers (pressing ENTER) cause the menu to redisplay,
# so .... back around the loop
# We only make it to the "continue" bit if the "test"
# program ("[") returned 0 (True)
#
[ "$ans" = "" ] && continue
#
# Decide what to do
#
case $ans in
1)
#
# Read in the contact details from the keyboard
#
echo "Please enter the following contact details:"
echo
echo "Given name: \c"
read name
echo " Surname: \c"
read surname
echo " Address: \c"
read address
echo " City: \c"
read city
echo " State: \c"
read state
echo " Zip: \c"
read zip
#
# Write the details to the text file
#
echo $name:$surname:$address:$city:$state:$zip >> $fname
;;
2)
#
# Show what's currently in the file
#
(
echo
echo Here are the current contacts in the database:
echo
echo "First Name Surname Address City
State Zip"
echo
"============================================================================"
#
# Display the line correctly formatted.
# Use awk for the formatting.
# The "-F :" bit causes awk to perceive fields as being
# separated by colons.
# "%-14.14s" means display a string in a field width
# of 14, left-justified.
#
sort -t : +1 $fname | awk -F : '{printf("%-14.14s%-16.16s%-20.20s%-
15.15s%-6.6s%-5.5s\n", $1, $2, $3, $4, $5, $6)}'
) | more
#
# And display how many there are
#
echo
echo There are `cat $fname | wc -l` contacts in the database
;;
3)
echo The Search case is not implemented yet
;;
4)
echo The Delete case is not implemented yet
;;
q*|Q*)
exit 0
;;
*)
echo "please enter a number between 1 and 4"
;;
esac
#
# Pause to give the user a chance to see what's on the screen
#
echo "Hit <ENTER> to continue: \c"
read junk
done
######################################################################
CHAPTER 8
#######
# avg #
#######
average()
{
if [ $# = 0 ]
then
echo 0
return
fi
total=0
for i
do
total=`expr $total + $i`
done
expr $total / $#
}
################
# func_library #
################
#
# yesno()
#
# A function to display a string (passed in as $*), followed by a "(Y/N)?",
# and then ask the user for either a Yes or No answer. Nothing else is
# acceptable.
# If Yes is answered, yesno() returns with an exit code of 0 (True).
# If No is answered, yesno() returns with an exit code of 1 (False).
#
yesno()
{
#
# Loop until a valid response is entered
#
while :
do
#
# Display the strings/paramters passed in, followed by "(Y/N)?"
# The \c causes suppression of echo's newline
#
echo "$* (Y/N)? \c"
#
# Read the answer - only the first word of the answer will
# be stored in "yn". The rest will be discarded
# (courtesy of "junk")
#
read yn junk
case $yn in
y|Y|yes|Yes|YES)
return 0;; # return TRUE
n|N|no|No|NO)
return 1;; # return FALSE
*)
echo Please answer Yes or No.;;
#
# and continue around the loop ....
#
esac
done
}
##############
# test_yesno #
##############
. func_library
#
#
#
while true
do
if yesno Do you really wish to quit now
then
exit
fi
done
####
##################################################################################
CHAPTER 9
#############
# usage_msg #
#############
usage()
{
script=$1
shift
#
# A simple test for the above function
#
usage $0 filename username ...
############
# contacts #
############
#!/bin/sh
# The above line causes this script to be run using the Bourne Shell (sh)
#######################################################################
#
# Script to maintain a contacts database.
#
#######################################################################
#
# Define the name of the file
#
fname=names.dat
#
# pause()
#
# Ask the user to press ENTER and wait for them to do so
#
pause()
{
echo "Hit <ENTER> to continue: \c"
read junk
}
#
# yesno()
#
# A function to display a string (passed in as $*), followed by a "(Y/N)?",
# and then ask the user for either a Yes or No answer. Nothing else is
# acceptable.
# If Yes is answered, yesno() returns with an exit code of 0 (True).
# If No is answered, yesno() returns with an exit code of 1 (False).
#
yesno()
{
#
# Loop until a valid response is entered
#
while :
do
#
# Display the string passed in in $1, followed by "(Y/N)?"
# The \c causes suppression of echo's newline
#
echo "$* (Y/N)? \c"
#
# Read the answer - only the first word of the answer will
# be stored in "yn". The rest will be discarded
# (courtesy of "junk")
#
read yn junk
case $yn in
y|Y|yes|Yes|YES)
return 0;; # return TRUE
n|N|no|No|NO)
return 1;; # return FALSE
*)
echo Please answer Yes or No.;;
#
# and continue around the loop ....
#
esac
done
}
#
# usage
#
# Generic function to display a usage message and exit the program
# The ^G is the bell (beep) character
# "basename" is used to transform "/home/mark/contacts"
# into "contacts" (for example)
#
usage()
{
script=$1
shift
#
# do_create()
#
# Create records for our database
#
do_create()
{
#
# Loop until the user is sick of entering records
#
while :
do
#
# Inner loop: loop until the user is satisfied with
# this ONE record
#
while :
do
#
# Read in the contact details from the keyboard
#
clear
#
# Now confirm ...
#
clear
#
# Ask the user if they wish to create another record.
# The "break" bit will only be executed if the user
# answers "No"
#
yesno Create another record || break
done
}
#
# do_view()
#
# Display all records in the file, complete with headings, sorted,
# one page at a time
#
do_view()
{
clear
#
# Show what's currently in the file
#
(
echo
echo Here are the current contacts in the database:
echo
echo "First Name Surname Address City
State Zip"
echo
"============================================================================"
#
# Display the line correctly formatted.
# Use awk for the formatting.
# The "-F :" bit causes awk to perceive fields as being
# separated by colons.
# "%-14.14s" means display a string in a field width
# of 14, left-justified.
#
sort -t : +1 $fname | awk -F : '{printf("%-14.14s%-16.16s%-20.20s%-15.15s%-
6.6s%-5.5s\n", $1, $2, $3, $4, $5, $6)}'
) | more
#
# And display how many there are
#
echo
echo There are `cat $fname | wc -l` contacts in the database
}
#
# do_search()
#
do_search()
{
echo The Search case is not implemented yet
}
#
# do_delete()
#
do_delete()
{
echo The Delete case is not implemented yet
}
#
# START MAIN CODE HERE
#
#
# Check that there is exactly one argument.
# "$#" contains the number of command line arguments.
# "$0" is the name of the shell script (complete path as
# typed in on the command line (e.g. "/home/mark/menu")
#
[ $# == 1 ] || usage $0 filename # exits the program
#
# If we got here, they must have supplied a filename. Store it away
#
fname=$1
#
# Check if the filename represents a valid file.
#
if [ ! -f $fname ]
then
echo $1 does not exist
#
# Ask if it should be created
#
if yesno "Create it"
then
#
# Attempt to create it
#
> $fname
#
# Check if that succeeeded
#
if [ ! -w $fname ]
then
echo $1 could not be created
exit 2
fi
#
# Otherwise we're OK
#
else
#
# They didn't want to create it
#
exit 0
fi
elif [ ! -w $fname ] # it exists - check if it can be written to
then
echo Could not open $1 for writing
exit 2
fi
#
# Loop forever - until they want to exit the program
#
while true
do
#
# Display the menu
#
clear
echo "\n\t\tSHELL PROGRAMMING DATABASE"
echo "\t\t\tMAIN MENU"
echo "\nWhat do you wish to do?\n"
echo "\t1. Create records"
echo "\t2. View records"
echo "\t3. Search for records"
echo "\t4. Delete records that match a pattern"
echo
#
# Prompt for an answer
#
echo "Answer (or 'q' to quit)? \c"
read ans junk
#
# Empty answers (pressing ENTER) cause the menu to redisplay,
# so .... back around the loop
# We only make it to the "continue" bit if the "test"
# program ("[") returned 0 (True)
#
[ "$ans" = "" ] && continue
#
# Decide what to do
#
case $ans in
1) do_create;;
2) do_view;;
3) do_search;;
4) do_delete;;
#
# Pause to give the user a chance to see what's on the screen
#
pause
done