Computing.Net > Forums > Unix > Help with Bourne Shellscript (More)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with Bourne Shellscript (More)

Reply to Message Icon

Name: InNeedOfHelp
Date: May 11, 2003 at 15:21:09 Pacific
OS: Not sure :S
CPU/Ram: Not Sure :S
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: May 11, 2003 at 19:31:12 Pacific
Reply:

#!/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


0

Response Number 2
Name: InNeedOfHelp
Date: May 12, 2003 at 03:48:10 Pacific
Reply:

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 1

So the program runs, but is then exiting because you cannot input any parameters. PLEASE HELP, this is so frustrating!


0

Response Number 3
Name: David Perry
Date: May 12, 2003 at 04:21:18 Pacific
Reply:

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?


0

Response Number 4
Name: David Perry
Date: May 12, 2003 at 04:22:57 Pacific
Reply:

BTW, if you make the script executable

chmod ug+x shellscript1.sh

then you can execute it as

./shellscript1.sh parm1 parm2


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Help with Bourne Shellscript (More)

Help with Bourne Shellscript www.computing.net/answers/unix/help-with-bourne-shellscript/4997.html

Help with UNIX Question www.computing.net/answers/unix/help-with-unix-question/2465.html

Help with gcc www.computing.net/answers/unix/help-with-gcc/5280.html