27. Example of Shell Scripts


Shell Script #1
(all this info is typed exactly how it would appear in the file):

#! /usr/bin/sh
# *********************************************************
# * the line above tells what shell to use this file in (sets the shell *
# *********************************************************
# This will perform backups and restorations on the system
#
# >>>>>>>>>>>>>>>>>VARIABLES USED<<<<<<<<<<<<<<<<<<<<
# bckuptyp - the input after the “backup” command
# paul.vtoc - file that partition info is kept
# disk - holds the physical disk address
# slice - holds slice for backing up
# slice2 - holds slice info for restoring
#
# **********************************************************
# *this command will use the input from whatever was typed after the *
# * filename and assigns it to variable $1, then assigns it to the variable *
# * bckuptyp *
# **********************************************************
bckuptyp=$1
disk=c0t0d0
if [ $bckuptyp = “nightly” ]
then
echo Performing nightly backup
# *********************************************************
# *ufsdump command is for backing up, the 5 means a level 5 dump, the f *
# * specifies where you want your backup info to go, the u means that you *
# * want the level 5 dump to be relative to your other dumps (0-4) the next *
# * field relates to the f argument, the next field is what you want to be *
# * backed up. *
# *********************************************************
ufsdump 5fu /dev/rmt/0n /dev/dsk/${disk}s0
ufsdump 5fuo /dev/rmt/0 /dev/dsk/${disk}s7
echo “ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!”
echo “ ! Nightly backup complete !”
echo “ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!”
fi
if [ $bckuptyp = “full ” ]
then
echo Performing full backup
# *********************************************************
# *this command will read the partition table from t0 and put it into a file in *
# * /etc/paul.vtoc *
# *********************************************************
prtvtoc /dev/rdsk/c0t0d0s2> /etc/paul.vtoc
# *********************************************************
# *the for is to define the variable “slice” to equal those four items and to *
# * keep doing it until they are all used (when used in conjunction withthe do *
# *command. Then the ufsdump command is the same as above (the o *
# *argument means to eject the tape, and the /dev/rmt/0n the n means no *
# * rewind, leaving it off means rewind *
# *********************************************************
for slice in s0 s4 s5 s6
do
ufsdump 0fu /dev/rmt/0n /dev/dsk/$disk$slice
done
ufsdump 0fuo /dev/rmt/0 /dev/dsk/${disk}s7
echo “ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!”
echo “ ! Full backup complete !”
echo “ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!”
fi
# *********************************************************
# */dev/rdsk is for moving info a clock at a time and the c=controller which *
# * is the physical port it is connected to, t=target is the number assigned to *
# *the physical device, d=device the number of the device, and s=slice *
# *the partition number you are dealing with *
# *********************************************************
if [ $bckuptyp = “restore ” ]
then
echo Restoring System
# *********************************************************
# *this command will take the info from /etc/paul.vtoc and partition second *
# *disk t1, the disk variable is set to the disk that will be adjusted, then used*
# *for fmthard cmd with the slice that will be fixed. Then the newfs command*
# * is used to make a new file system. *
# *********************************************************
disk=c0t1d0
cd /tmp
platform= ‘uname -i’
ufsrestore x ./etc/paul.vtoc
fmthard -s ./etc/paul.vtoc /dev/rdsk/${disk}s2
installboot /usr/platform/$platform/lib/fs/ufs/bootblk
/dev/rdsk/${disk}s2
for slice in s0 s4 s5 s6 s7
do
echo y | newfs /dev/rdsk/$disk$slice
mount /dev/dsk/$disk$slice /a
cd /a
ufsrestore rf /dev/rmt/0n
cd /
umount /a
done
# *********************************************************
# *the echo y makes the computer think that the user is entering yes so it *
# *won’t prompt you every time for an answer. *
# *********************************************************
echo “ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!”
echo “ ! Restoration Complete !”
echo “ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
fi
# *********************************************************
# *the line below states that if the variable($backuptype) doesn’t equal (!) *
# *nightly and (-a) then echo. *
# *********************************************************
if [ $bckuptyp !=nightly –a $bckuptyp !=full -a $bckuptyp !=restore ]
then
echo “backup ! full | nightly | restore”
echo
echo usage: backup full
echo “ backup nightly”
echo “ backup restore”
fi


Shell Script #2

#! /usr/bin/sh
# This script was created to simplify adding users
#>>>>>>>>>>>>>>>>>>>>VARIABLE USED<<<<<<<<<<<<<<<<<<
# name - login name of user
# length - length of user name to check for errors
# invalid - checks name variable to make sure it is a-z 0-9
# u - unique user id (uid)
# gp - group number that the user belongs to
# f1 - first name of user for the gcos (comment field)
# l2 - last “ “
# invalid2 - checks to make sure that login name is unique
# invalid3 - checks userid in /etc/passdw to make sure it is unique
# invalid4 - checks the group number to make sure it is a valid group
# *********************************************************
# *The the \c voids the carriage return, leaving it after the echo statement .*
# * The read command inputs the variable name. *
# *********************************************************
echo “What would you like your login name to be <a-z 0-9>? \c”
# *********************************************************
# *The next line checks the length (btwn 1-8 characters) using the word *
# *count (wc) command, the back brackets make the variable length equal *
# *everything inside. *
# *********************************************************
length=’echo $name | wc -m’
#> >>>>>>This is the if then argument for the length<<<<<<<
if [ $length -le 9 -a $length -ge 2 ]
then
echo “\c”
else
echo Invalid username - must be 1-8 characters
exit 1
fi
# *********************************************************
# *Exit 1 means exit with error, regular exit would be a successful exit first *
# *line of if is [ variable name -less than or equal to (le) -and (a) variable *
# * name again –greater than or equal to (ge) the numbers after are the *
# * arguments 2-9 because the length variable includes the carriage return *
# *********************************************************
#>>>>This set tests to make sure the name is a-z 0-9<<<<<<
invalid= ‘echo $name | grep “[^a-z0-9]”`
# *********************************************************
# *^ = other than, again the ‘’ makes invalid =whatever they contain. The *
# *grep command checks the output of the echo to check that doesn’t = a-z *
# *or 0-9. The “” around the grep argument makes that the output which if *
# *the name was inputted correctly would = nothing *
# *********************************************************
# >>>>>This routine checks to make sure that the user name is valid<<<<<<
invalid= ‘echo $name | grep “^a-z0-9]”`
if [ “$invalid” !=”” ]
then
echo Your login name was not valid – must be a-z 0-9
echo Input a new login name
read name
fi
# *********************************************************
# *Square brackets [] checks for 1 character in the line. The line below *
# *checks the /etc/passwd file for something that matches the name variable *
# *********************************************************
invalid2= ‘grep $name /etc/passwd’
# *********************************************************
# *This routine checks to make sure that $name is a unique login name the *
# *quotes around the $invalid makes the $ = variable switch. The ! after *
# *invalid means that if invalid is blank then echo *
# *********************************************************
if [ “$invalid2” != “”]
then
echo Your login name was not unique
exit 1
fi
echo “What is your userid? \c”
read u
#>>>>>>>This is the if then argument for the length<<<<<<<
if [ $u -le 60001 -a $u -ge 100 ]
then
echo Valid number used
else
echo INVALID userid - must be between 101 and 60000
exit 1
fi
# *********************************************************
# *The next line will make the variable "invalid3" equal to the following: the *
# *‘ makes the output of the stuff the variable the cut takes out certain fields, *
# *-d shows the delimiter, -f chooses which field /etc/passwd is the file you *
# *want to manipulate then the pipe command " |" redirects the output, grep *
# *will show only things that are alike *
# *********************************************************
invalid3=`cut -d: -f3 /etc/passwd | grep $u’
if [ "$invalid3" != "" ]
then
echo You have not entered a unique userid
# *********************************************************
# *The next line is in quotes because the “’”makes the computer think that *
# *anything after userid is considered part of a command so ‘s are in use *
# *and every line below is considered a command which will result in an *
# *error. *
# *********************************************************
echo “Please enter a unique userid. The following userids are in use”
# *********************************************************
# *The cut command will cut the third field of /etc/password to show the *
# * user which user ids are inuse (-d shows how fields are separated *
# *********************************************************
cut –d: -f3 /etc/passwd | more
exit 1
fi
echo "What is your group number? \c" ; read gp
invalid4 =`cut –d: -f4 /etc/passwd | grep $gp’
if [ "$invalid4 = "" ]
then
echo You have not entered a valid groupnumber
echo Please enter one of the following group numbers
cut –d: -f4 /etc/passwd | more
exit 1
fi
echo "What is your first name? \c"; read f1
echo "What is your last name? \c"; read f2
echo "***********************WARNING**********************"
echo "*Your password is set to your last name, exactly as you typed it. *"
echo "* Please change it as soon as you login. *"
echo "*******************************************************"
# *********************************************************
# *The first line sets the password as the users last name, The second line is *
# *the salutation to the user, The last line adds the user, using the variables *
# *after the switch. *
# *********************************************************
passwd -d $12
echo “Thank you. Your account will be added ASAP”
# this line adds the user using the variables after the switch
useradd -u $u -g $gp -c "$f1 $12"” -s /usr/bin/ksh -d /export/home/$12 -m $name


  Go to Chapter 26       Go to Index