LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Script error: "syntax error near unexpected token 'then'" (https://www.linuxquestions.org/questions/linux-server-73/script-error-syntax-error-near-unexpected-token-then-885424/)

RyuuzakiMasato7 06-09-2011 09:47 AM

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? ><

catkin 06-09-2011 09:58 AM

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” ];

SL00b 06-09-2011 09:58 AM

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

AlucardZero 06-09-2011 09:59 AM

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.

RyuuzakiMasato7 06-13-2011 11:26 AM

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. ><

SL00b 06-13-2011 01:50 PM

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.

RyuuzakiMasato7 06-13-2011 02:08 PM

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


colucix 06-13-2011 02:29 PM

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.

RyuuzakiMasato7 06-16-2011 06:48 AM

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 ><'

colucix 06-16-2011 07:56 AM

I cannot see any non-matching " in the script above. Can you post the updated version of your script?

RyuuzakiMasato7 06-16-2011 10:17 AM

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.

SL00b 06-16-2011 10:44 AM

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.

RyuuzakiMasato7 06-16-2011 03:01 PM

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


colucix 06-16-2011 03:10 PM

Indeed it looks right. Every double quote pair is perfectly matched. I really don't know what happens. :confused:

Reuti 06-16-2011 03:20 PM

What about executing the script with debugging on, i.e.
Code:

#!/bin/sh -x
as first line.


All times are GMT -5. The time now is 12:05 PM.