LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Closed Thread
 
Search this Thread
Old 12-28-2011, 02:30 PM   #1
smecnb
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Rep: Reputation: Disabled
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!”

Last edited by colucix; 12-28-2011 at 03: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.
Old 12-28-2011, 02:39 PM   #2
zQUEz
Member
 
Registered: Jun 2007
Distribution: Fedora, RHEL, Centos
Posts: 294

Rep: Reputation: 53
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`.

Last edited by zQUEz; 12-28-2011 at 02:45 PM.
 
1 members found this post helpful.
Old 12-28-2011, 02:50 PM   #3
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,790

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
@ 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.

Last edited by T3RM1NVT0R; 12-28-2011 at 02:52 PM.
 
1 members found this post helpful.
Old 12-28-2011, 03:11 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.

Last edited by colucix; 12-28-2011 at 03:13 PM.
 
1 members found this post helpful.
Old 12-28-2011, 05:48 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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 01:47 PM. Reason: 1. forgot about the PS3 feature in select 2. fixed a coupla errors
 
4 members found this post helpful.
Old 12-28-2011, 06:15 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by David the H. View Post
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.
 
Old 12-28-2011, 08:07 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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]".
 
1 members found this post helpful.
Old 12-29-2011, 02:54 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Ok... understood, now. Sorry.
 
Old 12-30-2011, 12:13 PM   #9
smecnb
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-30-2011, 01:06 PM   #10
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,790

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
@ 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.

Last edited by T3RM1NVT0R; 12-30-2011 at 01:12 PM.
 
Old 02-24-2013, 09:23 PM   #11
praneshb01
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
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>

Last edited by colucix; 02-25-2013 at 01:42 AM. Reason: Driving traffic to your own blog site is not allowed at LQ.
 
Old 02-25-2013, 01:44 AM   #12
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
@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.).
 
  


Closed Thread

Tags
bash, case, select


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
Error in Bash: line 77: syntax error: unexpected end of file bribon Programming 8 07-13-2011 01:43 PM
-bash: *.sh: line 25: syntax error: unexpected end of file prashanth212 Linux - General 8 04-06-2010 12:52 AM
bash line 74: syntax error: unexpected end of file help? andycol Linux - General 5 09-14-2009 09:12 AM
Bash script -----------syntax error: unexpected end of file ArthurHuang Programming 2 05-01-2009 11:29 AM
Bash script - syntax error: unexpected end of file Mr Pink Programming 7 12-19-2008 07:31 AM


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

Main Menu
Advertisement
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