Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I am trying to put together a script (see below) that will backup data to a removable HHD by date. After the first input, the unexpected-end-of-file error points to line #37 . . . only there is no line #37. I assume, then, that something is wrong in the whole durn thing. BTW, adding "fi" at line #37 results in line #38 being the culprit. Any help/explanation/suggestions would be GREATLY appreciated. Thanx in advance.
Code:
#!/bin/bash
clear
echo “Back up your data? Type y or n and press [ENTER]”
read answer1
if [[ $answer1 == “N” || $answer1 == “n” ]]; then
echo “Goodbye” -t 5
else
if [[ $answer1 == “Y” || $answer == “y” ]]; then
echo “Please confirm backup. Type y or n and press [ENTER]” -t 10
read answer2
if [[ $answer2 == “N” || $answer2 == “n” ]]; then
echo “Goodbye” -t 5
else
if [[ $answer2 == “Y” || $answer == “y” ]]; then
if [$(date +%u) -eq 1]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Monday
else
if [[ $answer2 == “Y” || $answer == “y” ]]; then
if [$(date +%u) -eq 2]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Tuesday
else
if [[ $answer2 == “Y” || $answer == “y” ]]; then
if [$(date +%u) -eq 3]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Wednesday
else
if [[ $answer2 == “Y” || $answer == “y” ]]; then
if [$(date +%u) -eq 4]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Thursday
else
if [[ $answer2 == “Y” || $answer == “y” ]]; then
if [$(date +%u) -eq 5]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Friday
echo “BACKUP COMPLETE! PLEASE UNMOUNT DEVICE, REMOVE CARTRIDGE AND STORE IN THE SAFE. SEE YOU NEXT TIME!”
Last edited by colucix; 12-28-2011 at 02:06 PM.
Reason: Added code tags for readability
Click here to see the post LQ members have rated as the most helpful post in this thread.
I would request you to use quote or code to keep the indenting of the script when you are posting in the thread. First thing that I would like to point is that you should use elif instead of else because there is no point using else with a condition.
Code:
#!/bin/bash
clear
echo “Back up your data? Type y or n and press [ENTER]”
read answer1
if [[ $answer1 == “N” || $answer1 == “n” ]]; then
echo “Goodbye” -t 5
elif [[ $answer1 == “Y” || $answer == “y” ]]; then
echo “Please confirm backup. Type y or n and press [ENTER]” -t 10
read answer2
if [[ $answer2 == “N” || $answer2 == “n” ]]; then
echo “Goodbye” -t 5
elif [[ $answer2 == “Y” || $answer == “y” ]]; then
if [ $(date +%u) -eq 1 ]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Monday
elif [ $(date +%u) -eq 2 ]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Tuesday
elif [ $(date +%u) -eq 3 ]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Wednesday
elif [ $(date +%u) -eq 4 ]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Thursday
elif [ $(date +%u) -eq 5 ]; then
cp /home/XXXXDATA -R /dev/xxxxbkp/Friday
echo “BACKUP COMPLETE! PLEASE UNMOUNT DEVICE, REMOVE CARTRIDGE AND STORE IN THE SAFE. SEE YOU NEXT TIME!”
fi
fi
fi
I hope the above script should work the way you want it to be.
Last edited by T3RM1NVT0R; 12-28-2011 at 01:52 PM.
I agree with T3RM1NVT0R. The main problem of your code is that there is no fi keyword to close the if/then/else constructs, hence the unexpected end error! See: http://linuxcommand.org/wss0090.php#if.
No... Please DO NOT use [quote][/quote] tags around code! They are only supposed to be used to repeat and highlight something another person has said. They do not preserve formatting in any way.
Please only use [code][/code] tags around your code and data. code tags preserve all whitespace and formatting, and display the data in a nice monospace font.
Anyway, another possible problem with the above is the quotation marks used, at least as displayed above. Your shell only treats standard ascii ', ", and ` as special. None of the other fancy-style quotation marks can be used in shell syntax patterns.
Code:
if [[ $answer1 == “N” || $answer1 == “n” ]]; then ## incorrect
if [[ $answer1 == "N" || $answer1 == "n" ]]; then ## correct
But anyway, case statements are generally better for evaluating the input from read. It's also common to embed your menus in a never-ending while loop, so that it keeps repeating until you get the input you want and force it to break.
Example code:
Code:
while true; do
read -p "Exit this loop? [y/n]: " answer
case $answer in
y|Y*) echo "Ok, exiting now. Goodbye."
break
;;
n|N*) echo "What, you want to try again?"
continue #not really necessary here
;;
*) echo "Huh? What did you say? It's Yes or No!"
;;
esac
done
You can also use a select statement for simple menus, which works almost the same as above.
Code:
PS3="Exit this loop [choose 1 or 2]? "
select answer in Yes No; do
case $answer in
Yes) echo "Ok, exiting now. Goodbye."
break
;;
No) echo "What, you want to try again? "
;;
*) echo "Huh? What did you say? It's Yes or No!"
;;
esac
done
Last edited by David the H.; 12-29-2011 at 12:47 PM.
Reason: 1. forgot about the PS3 feature in select 2. fixed a coupla errors
No... Please DO NOT use [quote][/quote] tags around code! They are only supposed to be used to repeat and highlight something another person has said. They do not preserve formatting in any way.
Hey David, maybe I'm too tired tonight, but I cannot see any quoted code here, nor any editing after your post. Please, show me exactly what do you refer to and I will fix it. Thank you.
Thank you all so much for your input! T3RM1NVT0R, I will try these excellent corrections asap. The intended structure/indents become so much clearer using 'elif' rather than 'else' and 'if', as well as the proper locations for 'fi'. Thank you for this introduction to it's use and proper indentation.
zQUEz, I will keep this in mind if it bombs out as is. Thanx.
colucix, thanks for your corroboration and the link. It is a great help.
David, your terrific examples have already generated ideas for the expansion/improvement of the original script! As for the quotes . . . not sure what happened. I merely copied/pasted the script here and it looked fine at that point with standard ASCII quotes. Many thanks!
Thanks again to all. And to you and yours, I wish the very best for the coming new year.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.