Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
ENDINPUTread 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 dirbackup='ls | grep backup' #assigns backup variable
if [ "$backup" != "" ]
thenecho "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
;;
esacelse [ "$backup" = "" ]
mkdir backup
echo "Making backup directory..."
echo ""
echo ""DIR_LIST=`ls *.*`
COPY_LIST="$DIR_LIST"
for i in $COPY_LIST
dofor j in $i
doecho "Copying $j to backup..."
cp -f ~/$j ~/backup/$jdone
done
fi
;;2)
date
;;3)
cal
;;4)
ls -la
;;*)
print "Invalid option, Please try again. Use 1-4"
;;esac
exit 0

>> 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_LISTthe above 3 lines just says
for i in *.*also, no need to use `ls *.*` with a for loop.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |