[SOLVED] Script error: "syntax error near unexpected token 'then'"
Linux - ServerThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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'
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. ><
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
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.