LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 06-09-2011, 09:47 AM   #1
RyuuzakiMasato7
LQ Newbie
 
Registered: Jun 2011
Posts: 8

Rep: Reputation: Disabled
Question 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? ><
 
Old 06-09-2011, 09:58 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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.
 
Old 06-09-2011, 09:58 AM   #3
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 111Reputation: 111
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.
Old 06-09-2011, 09:59 AM   #4
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,647

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
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.
 
Old 06-13-2011, 11:26 AM   #5
RyuuzakiMasato7
LQ Newbie
 
Registered: Jun 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
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. ><
 
Old 06-13-2011, 01:50 PM   #6
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 111Reputation: 111
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.
 
Old 06-13-2011, 02:08 PM   #7
RyuuzakiMasato7
LQ Newbie
 
Registered: Jun 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-13-2011, 02:29 PM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 06-16-2011, 06:48 AM   #9
RyuuzakiMasato7
LQ Newbie
 
Registered: Jun 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
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 ><'
 
Old 06-16-2011, 07:56 AM   #10
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I cannot see any non-matching " in the script above. Can you post the updated version of your script?
 
Old 06-16-2011, 10:17 AM   #11
RyuuzakiMasato7
LQ Newbie
 
Registered: Jun 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
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.
 
Old 06-16-2011, 10:44 AM   #12
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 111Reputation: 111
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.
 
Old 06-16-2011, 03:01 PM   #13
RyuuzakiMasato7
LQ Newbie
 
Registered: Jun 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-16-2011, 03:10 PM   #14
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Indeed it looks right. Every double quote pair is perfectly matched. I really don't know what happens.
 
Old 06-16-2011, 03:20 PM   #15
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,320

Rep: Reputation: 252Reputation: 252Reputation: 252
What about executing the script with debugging on, i.e.
Code:
#!/bin/sh -x
as first line.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
./script.sh: line 2: syntax error near unexpected token `(' n00balert Programming 3 03-11-2010 04:22 PM
syntax error near unexpected token "fi" phillip58 Programming 3 07-05-2009 01:00 PM
syntax error unexpected token near "then" pclimmex Linux - Newbie 1 06-20-2008 10:51 PM
Backup Script error "line 31: syntax error: unexpected end of file" eswanepoel General 7 12-07-2007 09:28 AM


All times are GMT -5. The time now is 08:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration