Help with Assignment

March 22, 2009 at 14:57:17
Specs: Sun Solaris 2.8
I need help with following ksh script - it is only partially working - any comments appreciated!

Code below:

#!/bin/ksh
#
# Name: Cheryl.ksh
#
# Menu driven korn shell that checks for the existence of a backup directory
# - creates it if it doesn't exist - and copies files with extentions
# from home directory to backup directory.
#
# Option 2 displays date/time
#
# Option 3 displays current month calendar
#
# Option 4 display a long listing of current directory
#
# Date March 18, 2009
#
# Written by: Cheryl Simpson
#
#######################################################################
print ""
print ""
print " Select a Menu Option"

cat << ENDINPUT #This reads in input

1. Make a backup directory of your home directory
2. Print Current Date and Time
3. Print Current Month Calendar
4. Do a long listing of current directory
ENDINPUT

read option

# this kind of code will let you display a menu and get input.
# and then use a case statmet to do each menu item selected.

case $option in
1)
cd ~ #changes dir to current user's home dir

backup='ls | grep backup' #assigns backup variable

if [ "$backup" != "" ]
then

echo "Backup directory already exists - would you like to delete?"
echo "Please type yes or no"

read answer

case $answer in

yes) rm -R backup
;;

no)
echo "Backup directory not removed"
exit 0
;;
esac

else [ "$backup" = "" ]
mkdir backup
echo "Making backup directory..."
echo ""
echo ""

DIR_LIST=`ls *.*`

COPY_LIST="$DIR_LIST"

for i in $COPY_LIST
do

for j in $i
do

echo "Copying $j to backup..."
cp -f ~/$j ~/backup/$j

done

done
fi
;;

2)
date
;;

3)
cal
;;

4)
ls -la
;;

*)
print "Invalid option, Please try again. Use 1-4"
;;

esac

exit 0


See More: Help with Assignment

Report •


#1
March 22, 2009 at 19:07:59
>> ls | grep backup'

no need to use grep if you are listing files. Use shell expansion like this

ls *backup*

>> DIR_LIST=`ls *.*`
>>COPY_LIST="$DIR_LIST"
>> for i in $COPY_LIST

the above 3 lines just says

for i in *.*

also, no need to use `ls *.*` with a for loop.


Report •
Related Solutions


Ask Question