Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I posted a message a couple of days ago asking for some help with a Bourne shellscript, and I have now written some code, but am getting an error, which is "shellscript.sh: syntax error at line 33: `end of file' unexpected" - and I don't know how to fix it and get my script working!
I was wondering if anyone could help me – if they could I would be extremely grateful!
Basically, I want to write a Bourne Shellscript program that allows a user to backup a directory and all the files and directories below it. I want 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 want the program to output a suitable error messages and exit.
Assuming the user did enter exactly two parameters, I want the program to then check to see that the directory referred to in the first parameter exists. If it does not, again the program should output suitable error messages and exit. If the directory does exist, I want the program to back it up to the directory given in the second parameter. The program should also take appropriate action if the directory given as the second parameter already exists, allowing for the fact that the user still wants to make the backup but that you should not destroy another backup.
My code is...
#!/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

#!/bin/sh
set -x
ECHO=`which echo`
if [ $# -ne 2 ] ; then
$ECHO "Sorry, you must enter two parameters exactly"
$ECHO "Usage: $0 source_dir target_dir"
exit 1
else
if [ ! -d $1 ] ; then
$ECHO "The directory [$1] does not exist - Aborting"
exit
else
if [ -d $2 ] ; then
$ECHO "The directory [$2] already exists"
$ECHO "Do you want to overwrite the directory? ( y/n ) :\c"
read answer
echo ""
answer=`$ECHO $answer | cut -c 1 | tr '[a-z]' '[A-Z]'`
fi
if [ "$answer" != "Y" ] ; then
$ECHO "Aborting"
exit
fi
cp -R $1 $2 #Backs up the directories mentioned in the command line
fi
fi

Thanks for the code David, the good news is that the program runs when inputting sh shellscript1.sh. However, the bad news is that the following error then occurs...
+ which echo
ECHO=/bin/echo
+ [ 0 -ne 2 ]
+ /bin/echo Sorry, you must enter two parameters exactly
Sorry, you must enter two parameters exactly
+ /bin/echo Usage: shellscript1.sh source_dir target_dir
Usage: shellscript1.sh source_dir target_dir
+ exit 1So the program runs, but is then exiting because you cannot input any parameters. PLEASE HELP, this is so frustrating!

As the message says, enter the command as
shellscript1.sh source_dir target_dir
where source_dir is where you want to copy from and target_dir is where you want to copy to. You asked for a script that takes command line arguements, right?

BTW, if you make the script executable
chmod ug+x shellscript1.sh
then you can execute it as
./shellscript1.sh parm1 parm2

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

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