| Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
06-09-2011, 09:47 AM
|
#1
|
|
LQ Newbie
Registered: Jun 2011
Posts: 8
Rep: 
|
Script error: "syntax error near unexpected token 'then'"
Hi all, very new at this and completely stumped as to what's gone wrong here. I'm trying to get this script working, it's supposed to give me options of typing B to backup a folder, typing R to restore the backup, and typing E to exit.
Code:
#!/bin/sh
backup=”B”
restore=”R”
exit=”E”
# backup all files from a specified location to a date stamped folder
echo –n “To backup the backups folder, type B and press Enter.”
echo –n “To restore the backup, type R and press Enter.”
echo –n “To exit, type E and press Enter.”
read entry
if [ “entry” == “backup” ];
then
tar zc /home/Rory/backups -f /home/Rory/archive/Rory-`date+%d%m%y`.tar.gz; then
echo –n “Backup complete.”
echo –n “To backup the backups folder, type B and press Enter.”
echo –n “To restore the backup, type R and press Enter.”
echo –n “To exit, type E and press Enter.”; then
read entry
#“Rory-`date+%d%m%y`.tar.gz” creates “Rory-20May2011.tar.gz” etc.
if [ “entry” == “restore” ]; then
tar –xvwzf Rory-`date+%d%m%y`.tar.gz; then
echo –n “Restore complete.”
echo –n “To backup the backups folder, type B and press Enter.”
echo –n “To restore the backup, type R and press Enter.”
echo –n “To exit, type E and press Enter.”; then
read entry
#restores backup
If [ “entry” == “exit” ]; then
Exit
# Exits the program
else
echo –n “Invalid entry. Please type B, R or E.”; then
read entry
fi
When I attempt to run the script, I get my echos prompting me to type B, R or E and press enter. When I type B and press Enter, I get this:
Code:
line 18: syntax error near unexpected token 'then'
line 18: 'then'
Can anyone help a newbie out? ><
|
|
|
|
06-09-2011, 09:58 AM
|
#2
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
It's not on line 18 in the posted script but the then on tar zc /home/Rory/backups -f /home/Rory/archive/Rory-`date+%d%m%y`.tar.gz; then is not right.
EDIT: and if [ “entry” == “backup” ]; shuold be if [ “$entry” == “backup” ];
Last edited by catkin; 06-09-2011 at 10:00 AM.
|
|
|
|
06-09-2011, 09:58 AM
|
#3
|
|
Member
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375
Rep: 
|
Quote:
|
tar zc /home/Rory/backups -f /home/Rory/archive/Rory-`date+%d%m%y`.tar.gz; then
|
Duplicate instruction bolded. It's also not the only time you did it. This mistake occurs multiple times in the script.
Here's a simple example of how to code a conditional in bash: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html
|
|
|
1 members found this post helpful.
|
06-09-2011, 09:59 AM
|
#4
|
|
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,474
|
Look at line 18?
Code:
tar zc /home/Rory/backups -f /home/Rory/archive/Rory-`date+%d%m%y`.tar.gz; then
That then should not be there.
|
|
|
|
06-13-2011, 11:26 AM
|
#5
|
|
LQ Newbie
Registered: Jun 2011
Posts: 8
Original Poster
Rep: 
|
Thanks for the replies guys - I removed all the "then"s and added the $ where required, but although that particular error is gone, I'm now getting the same error again but for "fi" instead of "then". And yet again, I'm completely lost. ><
|
|
|
|
06-13-2011, 01:50 PM
|
#6
|
|
Member
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375
Rep: 
|
As the link I gave demonstrates, the syntax of a bash conditional statement is as follows:
Code:
if [ test ]; then
<commands to run if true>
fi
Or,
Code:
fi [ test ]; then
<commands to run if true>
else
<commands to run if false>
fi
The function of fi is to mark the end of the conditional statement.
If you need more specific assistance, please post the updated version of your script.
|
|
|
|
06-13-2011, 02:08 PM
|
#7
|
|
LQ Newbie
Registered: Jun 2011
Posts: 8
Original Poster
Rep: 
|
Still pretty lost >< This is how my code looks right now:
Code:
#!/bin/sh
backup=”B”
restore=”R”
exit=”E”
echo –n “To backup the backups folder, type B and press Enter.”
echo –n “To restore the backup, type R and press Enter.”
echo –n “To exit, type E and press Enter.”
read entry
if [[ “$entry” == “backup” ]];
tar zc /home/Rory/backups -f /home/Rory/archive/Rory-`date+%d%m%y`.tar.gz;
echo –n “Backup complete.”
echo –n “To backup the backups folder, type B and press Enter.”
echo –n “To restore the backup, type R and press Enter.”
echo –n “To exit, type E and press Enter.”;
fi
read entry
if [ “$entry” == “restore” ];
tar –xvwzf Rory-`date+%d%m%y`.tar.gz;
echo –n “Restore complete.”
echo –n “To backup the backups folder, type B and press Enter.”
echo –n “To restore the backup, type R and press Enter.”
echo –n “To exit, type E and press Enter.”;
fi
read entry
if [ “$entry” == “exit” ];
exit
echo –n “Invalid entry. Please type B, R or E.”; then
read entry
fi
fi
|
|
|
|
06-13-2011, 02:29 PM
|
#8
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
This is a corrected version of your script (please check the differences):
Code:
#!/bin/sh
backup="B"
restore="R"
exit="E"
echo "To backup the backups folder, type B and press Enter."
echo "To restore the backup, type R and press Enter."
echo "To exit, type E and press Enter."
read entry
if [[ "$entry" == "$backup" ]]
then
tar zc /home/Rory/backups -f /home/Rory/archive/Rory-`date +%d%m%y`.tar.gz
echo "Backup complete."
echo "To backup the backups folder, type B and press Enter."
echo "To restore the backup, type R and press Enter."
echo "To exit, type E and press Enter.";
fi
read entry
if [[ "$entry" == "$restore" ]]
then
tar -xvwzf Rory-`date +%d%m%y`.tar.gz
echo "Restore complete."
echo "To backup the backups folder, type B and press Enter."
echo "To restore the backup, type R and press Enter."
echo "To exit, type E and press Enter."
fi
read entry
if [[ "$entry" == "$exit" ]]
then
exit
else
echo "Invalid entry. Please type B, R or E."
read entry
fi
Anyway, if I were you I would consider the case/esac construct to avoid multiple if/then. Moreover it will work as you expect if you use a loop to read the user input multiple times, instead of repeating it randomly inside the script.
|
|
|
|
06-16-2011, 06:48 AM
|
#9
|
|
LQ Newbie
Registered: Jun 2011
Posts: 8
Original Poster
Rep: 
|
Thanks for the corrections colucix - the old errors are now gone, but I'm now getting a new one:
Code:
line 45: unexpected EOF while looking for matching "
line 48: syntax error: unexpected end of file
I've been trying to figure this out and fix it but nothing I try is working ><'
|
|
|
|
06-16-2011, 07:56 AM
|
#10
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
I cannot see any non-matching " in the script above. Can you post the updated version of your script?
|
|
|
|
06-16-2011, 10:17 AM
|
#11
|
|
LQ Newbie
Registered: Jun 2011
Posts: 8
Original Poster
Rep: 
|
I'm using the corrected version of the script that you have already posted, I've checked it over several times and mine is definately the same.
|
|
|
|
06-16-2011, 10:44 AM
|
#12
|
|
Member
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375
Rep: 
|
This is line 45 in the corrected script:
Code:
echo "Invalid entry. Please type B, R or E."
Looks like the quotes match up to me.
Again... post what you have.
|
|
|
|
06-16-2011, 03:01 PM
|
#13
|
|
LQ Newbie
Registered: Jun 2011
Posts: 8
Original Poster
Rep: 
|
Alright, but it's exactly the same as a few posts above:
Code:
#!/bin/sh
backup="B"
restore="R"
exit="E"
echo "To backup the backups folder, type B and press Enter."
echo "To restore the backup, type R and press Enter."
echo "To exit, type E and press Enter."
read entry
if [[ "$entry" == "$backup" ]]
then
tar zc /home/Rory/backups -f /home/Rory/archive/Rory-`date +%d%m%y`.tar.gz
echo "Backup complete."
echo "To backup the backups folder, type B and press Enter."
echo "To restore the backup, type R and press Enter."
echo "To exit, type E and press Enter.";
fi
read entry
if [[ "$entry" == "$restore" ]]
then
tar -xvwzf Rory-`date +%d%m%y`.tar.gz
echo "Restore complete."
echo "To backup the backups folder, type B and press Enter."
echo "To restore the backup, type R and press Enter."
echo "To exit, type E and press Enter."
fi
read entry
if [[ "$entry" == "$exit" ]]
then
exit
else
echo "Invalid entry. Please type B, R or E."
read entry
fi
|
|
|
|
06-16-2011, 03:10 PM
|
#14
|
|
Moderator
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.4 OpenSuSE 12.2
Posts: 9,896
|
Indeed it looks right. Every double quote pair is perfectly matched. I really don't know what happens. 
|
|
|
|
06-16-2011, 03:20 PM
|
#15
|
|
Senior Member
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 11.4
Posts: 1,314
|
What about executing the script with debugging on, i.e. as first line.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:33 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|