LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script syntax error: unexpected end of file? (http://www.linuxquestions.org/questions/linux-newbie-8/bash-script-syntax-error-unexpected-end-of-file-920928/)

smecnb 12-28-2011 02:30 PM

bash script syntax error: unexpected end of file?
 
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!”


zQUEz 12-28-2011 02:39 PM

I might be wrong, but I am always under the impression you need to leave whitespace between the "[]" in your if statement:
Code:

[$(date +%u) -eq 1]
should be
Code:

[ $(date +%u) -eq 1 ]
Also, is your date command correct? Normally I would use:
Code:

[ `date +%u` -eq 1 ]
. I'm not in a position to test right now, so there might not be anything wrong with yours - I am just not sure.

Finally, where are all your closing `fi`'s? You need one for each `if`.

T3RM1NVT0R 12-28-2011 02:50 PM

@ Reply
 
Hi smecnb,

Welcome to LQ!!!

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.

colucix 12-28-2011 03:11 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.

David the H. 12-28-2011 05:48 PM

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


colucix 12-28-2011 06:15 PM

Quote:

Originally Posted by David the H. (Post 4560418)
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.

David the H. 12-28-2011 08:07 PM

The OP has fixed it now, but the script was originally posted raw.

My comment was really in response to T3RM1NVT0R's post, where he recommended using "quote or code [tags]".

colucix 12-29-2011 02:54 AM

Ok... understood, now. Sorry.

smecnb 12-30-2011 12:13 PM

Thank you, all!
 
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.

T3RM1NVT0R 12-30-2011 01:06 PM

@ Reply
 
@ smecnb

You're welcome. Please mark the thread as solved.

Wish you the same for the new year!!! Enjoy linux!!!

@ David

Thanks for clearing quote and code tag stuff.

praneshb01 02-24-2013 09:23 PM

For new newbie and Ubuntu users
 
There are several reasons for throwing this error when working with the bash script and mostly its due to syntax error. But, sometimes even though every syntax is correct, you may still get this error. You may find this link helpful for the unexpected end of file error
<moderated>

colucix 02-25-2013 01:44 AM

@praneshb01: driving traffic to your own blog it's not allowed here at LinuxQuestions. For any doubt about the advertising policy, feel free to contact the LQ administrator, using this form: http://www.linuxquestions.org/questions/sendmessage.php. Thread closed (R.I.P.).


All times are GMT -5. The time now is 02:49 AM.