Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Im new to this and i was trying to

Status
Not open for further replies.

sbaci

Technical User
May 12, 2003
1
GB
Im new to this and i was trying to create a script that
allows a user to backup a directory and all the files and directories below it. I was trying to get the program to take exactly two parameters, the name of a source directory (to back up) and a destination directory (to back up to). If the user does not enter exactly two parameters, I wanted the program to output a suitable error messages and exit.

Thought i had it done and dusted but when i run it the following error message appears

syntax error at line 33: `end of file' unexpected" and i havnt a clue how to fix it, and get my script working, any help please?

#!/bin/sh #Path of the shell transient command

if [ "$1 $2" -ne "0" ]; then echo "Sorry, you must enter two parameters exactly"
exit 1
else
if [! -f $1 ]
then
echo "The directory [$1] does not exist - Aborting"
exit
else
'cp -R $1 $2' #Backs up the directories mentioned in the command line
fi
if [ -f $2 ]
then
echo "The directory [$2] already exists"
/usr/5bin/echo "Do you want to overwrite the directory? ( y/n ) :\c"
read answer
read $answer
echo ""
if [ "£answer"!= "y" ] && [ "$answer"!= "Y" ]
then
echo "Aborting"
exit
fi
fi
fi
 
Here's a start on what I see in this script.

I copied your script, made a few changes and ran it, with the proper syntax of: [scriptname] dir1 dir2 and it worked.

It still isn't working if only one directory is passed and it doesn't ask if you want to overwrite the directory if it already exists. It does work if you specify for the first directory a directory that does not exist (That is, it stops and tells you the directory does not exist. But you have to have two dirs specified.) But maybe this will put you on the right track.

I tested this on a Solaris 7 system.

I would suggest, also, that if you are doing a cp, that you use cp -pR. If you don't use the -p, the time stamp and permissions of the files you are copying will change and you probably don't want that.

Here's what I found:

the line: if [! -f $1 ], you need to add a space after the open square bracket and you need to change the f to a d (because you are talking about directories, not files).

The line: 'cp -R $1 $2', you need to delete the quote marks.

The line that begins: /usr/5bin/echo, you need to change "5bin" to sbin.
 
I've fixed-up the syntax errors....

#!/usr/bin/ksh
#----check usage

if [ $# -ne 2 ]
then
echo "Sorry, you must enter two parameters exactly"
exit
fi
#----check dirs
if [ ! -d $1 ]
then
echo "The directory [$1] does not exist - Aborting"
exit
elif [ -d $2 ]
then
echo "The directory [$2] already exists"
echo "Do you want to overwrite the directory? ( y/n ) :\c"
read ANSWER
#----case is better than if for handling user input
case $ANSWER in
y*|Y*) echo Directory $2 will be overwritten ;;
*) echo Aborting ;
exit ;;
esac
fi
#----Back up the directories mentioned in the command line
cp -R $1 $2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top