|

Here are the scripts I wrote while studying the UNIX part of the
Computeach 4250 project folder. They are all available for download
if you have access to a UNIX system. Remember to chmod +x these
scripts if you do download them. As usual these scripts are copyright
to myself, so please do not use them as your own work.
Download #!/bin/sh #First script for Computeach echo "Hello World, this is not my first script (not really)" ls
# end of script
Download #!/bin/sh #Second script for Computeach, showing the read command. echo -n "Please enter your first name - " read first echo -n "Please enter your surname - " read surname echo "$first $surname, Welcome to shell scripts"
#end of script
Download #!/bin/sh #Third shell script for Computeach, showing the use of command line args echo "The command line arguments given to this script were - " echo "$1 $2 $3 $4 $5 $5 $6 $7 $8 $9" echo "Script name - $0" #end of script
Download #!/bin/sh #Forth script for Computeach - Number of cmd args given to script echo "Number of command line arguments given - $#" #end of script
Download #!/bin/sh #Script 5 for Computeach - show the use of the shift command echo "The first command line arg is $1, all args - $*" shift echo "The first command line arg is $1, all agrs - $*" #end of script
Download #!/bin/sh #Script 6 for Computeach - if-fi statements if (lpstat) then echo "OK" else echo "NOT OK" fi #end of script
Download #!/bin/sh #Script 7 for Computeach - if-fi statements again if (ls) then echo "OK" else echo "NOT OK" fi #end of script
Download #!/bin/sh
# status - Example script showing the use of the while loop.
# The script tests every 60 seconds to see if the given
# user is still logged on
# use is: status
# Set up variable 'loginFlag' as a flag
loginFlag=1
while [ $loginFlag -eq 1 ]; do
if ( who | grep -s $1 >/dev/null ) ; then
sleep 60
else
loginFlag=0
fi
done
#user has logged out
echo "$1 has logged out"
#end of script
Download #!/bin/sh
# monthdays script for Computeach - display the number of days in the current month
month=`date +%m`
monthName=`date +%B`
case $month in
1 | 3 | 5 | 7 | 8 | 10 | 12)
days=31;
;;
4 | 6 | 9 | 11)
days=30;
;;
2)
year='date +%Y'
#simple leap year test
if ( `expr ($year % 4) == 0` ) then
days=29;
else
days=28;
fi
;;
*)
#should never get here
echo "Error in date utility"
exit 1
;;
esac
echo "The month of $monthName has $days days"
#end of script
Download #!/bin/sh
# watch - Example script showing the use of the until loop.
# The script tests every 60 seconds to see if the
# given user has logged in.
# use is: watch
# Set up login flag
loginFlag=0
until [ $loginFlag -eq 1 ]; do
if ( who -h | grep -s $1 >/dev/null ); then
loginFlag=1
else
sleep 60
fi
done
echo "$1 has logged in"
# end of script
Download #!/bin/sh
#finduser script for Computeach.
#Check if the given user [users] is logged in.
if [ $# -ge 1 ]; then
for user in $* ; do
if (who -h | grep -s $user >/dev/null) then
echo "$user is logged in"
else
echo "$user is not logged in"
fi
done
else
echo "No user name given"
fi
#end of script
Download #!/bin/sh
# Menu script for Computeach - show use of case statement
echo "MENU"
echo ""
echo "1. Display current system date"
echo "2. Display current working directory"
echo "3. Create a new directory"
echo ""
echo -n "Enter your choice - "
read choice
echo ""
case $choice in
1)
date
;;
2)
pwd
;;
3)
echo -n "Directory name - "
read dirName
mkdir $dirName
;;
*)
echo "Choice unknown"
;;
esac
#end of script
Download #!/bin/sh
# trash - A simple shell script that removes the given file
# from it's current location into a directory named
# /trashbin.
# usage -
# trash -l - list all files in /trashbin
# trash -x - extract the filename given from the /trashcan
# to the current directory (un-trash)
# trash -e - removes all files from /trashbin (empty trash)
if [ $# -lt 1 ]; then
echo ""
echo "trash: usage is as follows:-"
echo " trash - moves filename given to the trashbin."
echo " trash -l - List all files currently in trashbin."
echo " trash -x - Extract the filename given from the trashbin"
echo " to the current directory."
echo " trash -e - Remove all files from the trashbin."
echo ""
else
case $1 in
"-l")
echo "Files currently in the trashbin are:-"
ls -AFl /trashbin
;;
"-x")
if [ "X$2" != "X" ]; then
if [ ! -e "/trashbin/$2" ]; then
echo "Error: trash: File '$2' not found in trashbin."
exit 1
fi
echo "Extracting $2 from the trashbin to `pwd` ..."
mv /trashbin/$2 ./
else
echo "Error: trash: No filename given."
exit 1
fi
;;
"-e")
echo "Removing all files from the trashbin ..."
rm -Ri /trashbin/*
;;
*)
if [ ! -e "$1" ]; then
echo "Error: trash: File '$1' does not exist."
exit 1
else
echo "Moving $1 to the trashbin ..."
mv $1 /trashbin/$1
fi
;;
esac
fi
# end of script
Back Home
|