lack of understanding > annoying error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting lack of understanding > annoying error
# 1  
Old 05-18-2009
Error lack of understanding > annoying error

I'm working on a script I wrote called backup.sh
when I run it like this:
Code:
. ./backup.sh

I get this error:
Code:
ksh: ./backup.sh[124]: no closing quote

when I run it this way:
Code:
backup.sh

I get this error:
Code:
backup.sh: 28: Syntax error: end of file unexpected (expecting "fi")

I looked through the code over and over (the entire script) and can't find any mismatching quotes or badly nested if statements. I don't have a strong enough understanding to know why one way is better or worse, and why either of them should be handled differently by the shell (I'm guessing they're being handled differently, otherwise they wouldn't have different output, right?)

Any help would be greatly appreciated. In-case this requires a look at the code:
Code:
#!/bin/sh

toBackUpDir="./tmp" #DIRECTORY TO BE BACKED UP
toBackUp="$toBackUpDir/*" #PATH TO BE BACKED UP
landingDir="./download/" #PATH FOR *.TGZ BACKUP TO BE STORED
sql_bDir="./sql_backups/"

#ERROR CHECK, DOES SPECIFIED LANDING DIRECTORY EXIST; ANY OLD BACKUPS?
echo "Checking for specified landing directory: $landingDir"

if [ -d $landingDir ]
then
    echo "Directory $landingDir is present and ready to receive backup."

    echo "Checking that $landingDir does not contain old backup files."
    tarCheck=`ls $landingDir | grep -i *tar*` #CHECKS IF ANY TAR IS SITTING IN DESTINATION FOLDER
    echo "The variable \$tarCheck returns:" $tarCheck #TEST OF VARIABLE

        # ERROR CHECK, ANY PREVIOUS BACKUPS STILL HERE?
        if [ -z $tarCheck ]
        then
            echo "Continuing backup sequence, as there are no old tars in the backup directory $landingDir"
            work="STARTING"
            echo "BEFORE NESTED ELSE"
        else
            echo "I am not ready to backup, first download and delete the above mentioned contents of the $landingDir landing directory." 
            work="NOT starting"
            echo "AFTER NESTED ELSE"
        fi #ENDIF FOR ERROR CHECKING
    echo "AFTER NESTED IF"
else
    echo "Directory $landingDir does not exist, creating it now"
    work="STARTING"
    mkdir $landingDir
fi #ENDIF LANDING DIR. CHECK


# MAIN MODULE: PARAMETERS, ERROR CHECK, TAR, GZIP
if [ $work = "STARTING" ]
then
        #OLD WAY: #fileName=`date | sed -n s/ /_/g p | sed -n s/^/Backup_/p | sed -n s/$/\.tgz/p |  sed -n s/^
/$landingDir/p`  #NAME OF BACKUP FILE
        fileName=`date | sed -n s/ /_/g p`
        fileName=${landingDir}Backup_${fileName}.tgz #NEW WAY #NAME OF BACKUP FILE

        #ERROR CHECK, DOES LOCATION TO BE BACKED UP EXIST?
        echo "The variable \$toBackUpDir (directory of files to be backed up) is: $toBackUpDir"
        echo "Making sure above said target exists: $toBackUpDir"

                if [ -d $toBackUpDir ]
                then
                        echo "Directory $toBackUpDir does indeed exist and is present for backing up."
    
        #CONTINUE PROGRAM

            echo "The variable \$fileName will be:" $fileName
    
        #START SQL BACKUP + TGZ 
            #MYSQL LANDING DIR. CHECK
            echo "Checking if sql backup landing directory exists: $sql_bDir"
            if [ -d $sql_bDir ]
            then
                echo "Database backup directory exists"
            else
                echo "Database backup directory does not exist, creating it"
                mkdir $sql_bDir
            fi #ENDIF SQL DIR. CHECK

            # MYSQL BACKUP
                   sql_bName=`date | sed -n s/ /_/g p | sed -n s/^/SqlBackup_`
            echo "The variable \$sql_bName will be $sql_bName" #TEST OF SQL NAME VARIABLE
            #mysqldump -uxxxxxx -pxxxxxx --opt information_schema > /$sql_bDir/$sql_bName.tgz

            # MYSQL CLEAN-UP < 10 FILES
            # CODE TO CLEAN UP OLDEST OF LISTING (ls -la)
            # THAT AMOUNTS TO GREATER THAN 10 ITEMS

            echo "Below are the files placed in final back up:" #EXPLANATION FOR USER
            tar zcvf "$fileName" $toBackUp #CREATE TGZ
    
            #DECLARE BACKUP FINISHED
            echo " " #VISUAL PADDING
            echo "Final back-up ready for download in, including \"information_schema\" database: $landingDir"

            else
            echo "ERROR:"
            echo "    Directory $toBackUpDir does not exist."
            echo "    Make sure my starting parameters are properly set."
            echo "Backup sequence ended early."
        fi #ENDIF BACKUP DIR. CHECK

fi #ENDIF FOR MAIN MODULE


Last edited by jzacsh; 05-21-2009 at 10:52 AM..
# 2  
Old 05-18-2009
Code:
                if [ -d $toBackUpDir ]
                then
                        echo "Directory $toBackUpDir does indeed exist and is present for backing up."
#<- Insert missing fi here    
        #CONTINUE PROGRAM

# 3  
Old 05-18-2009
Quote:
Originally Posted by pludi
Code:
                if [ -d $toBackUpDir ]
                then
                        echo "Directory $toBackUpDir does indeed exist and is present for backing up."
#<- Insert missing fi here    
        #CONTINUE PROGRAM

That if statement is ended at the bottom of the script (the "fi" can be found in the third to last line)

The else statement that forms the if you're looking at is at the 8th to last line
# 4  
Old 05-18-2009
Can't find any missing " or ` but the line:
Code:
if [ -z $tarCheck ]

needs speech marks around the $tarCheck otherwise when it is empty the test will fail.

Not so critical but might help as the strings going into work have spaces in them I would put speech marks around $work in the line:
Code:
if [ $work = "STARTING" ]

The header of the script says /bin/sh and then you test it with ksh ./backup.sh, sh and ksh are of course different shells that treat some items differently.

What does running:
Code:
$ sh -n ./backup sh

report?
# 5  
Old 05-19-2009
Have you check this line?
>
#OLD WAY: #fileName=`date | sed -n s/ /_/g p | sed -n s/^/Backup_/p | sed -n s/$/\.tgz/p | sed -n s/^/$landingDir/p` #NAME OF BACKUP FILE
>
Though you add comment at front of this line. You see that the editor wrap your text
down. However, you should check whether it is a consequent or separate statement.
The better way is add another comment to the second line.

In other hand, you can try and do debuging.
Just put comment to all the inner IF cause and put echo statement to verify that the if cause has not conflict. You can put echo command to show the flow and the assign value of each variable. Then you just remove the comment line by line and run for test.
You will then find the invalid statement.
# 6  
Old 05-19-2009
Thanks for all your replies!

Quote:
Originally Posted by TonyFullerMalv
Can't find any missing " or ` but the line:
Code:
if [ -z $tarCheck ]

needs speech marks around the $tarCheck otherwise when it is empty the test will fail.
the script previously worked fine before this (I tested it in both cases), but for the sake of being thorough (and I trust most people's opinions over mine in this case) I've stuck the quotes in.

Quote:
Originally Posted by TonyFullerMalv
Not so critical but might help as the strings going into work have spaces in them I would put speech marks around $work in the line:
Code:
if [ $work = "STARTING" ]

The header of the script says /bin/sh and then you test it with ksh ./backup.sh, sh and ksh are of course different shells that treat some items differently.
ahh, my newbie mistake - i forgot that . / would be in my shell which happens to be ksh ! didn't make the connection. thanks!

Quote:
Originally Posted by TonyFullerMalv
What does running:
Code:
$ sh -n ./backup sh

report?
the above has the same result as just typing backup.sh into the terminal (i didn't know about -n option, noexec according to man, thanks!).

which, with all the above changes, is now:
Code:
./backup.sh: 28: Syntax error: end of file unexpected (expecting "fi")

Quote:
Originally Posted by tom_cmu
Have you check this line?
>
#OLD WAY: #fileName=`date | sed -n s/ /_/g p | sed -n s/^/Backup_/p | sed -n s/$/\.tgz/p | sed -n s/^/$landingDir/p` #NAME OF BACKUP FILE
>
Though you add comment at front of this line. You see that the editor wrap your text
down. However, you should check whether it is a consequent or separate statement.
The better way is add another comment to the second line.
The line you're referring to is indeed only one line, without any breaks (though I know what you're saying, their isn't anywhere I can stick a second comment).

Quote:
Originally Posted by tom_cmu
In other hand, you can try and do debuging.
Just put comment to all the inner IF cause and put echo statement to verify that the if cause has not conflict. You can put echo command to show the flow and the assign value of each variable. Then you just remove the comment line by line and run for test.
You will then find the invalid statement.
I actually did try doing this debugging - but not through out the entire script. Now its done and this is the code I used for the output I get:

CODE USED:
Code:
...
                         echo "BEFORE NESTED ELSE"#DEBUG
                 else
                         echo "I am not ready to backup, first download and delete the above mentioned contents of the $landingDir landing direct
ory."
                         work="NOT starting"
                         echo "AFTER NESTED ELSE"#DEBUG
                 fi #ENDIF FOR ERROR CHECKING
         echo "AFTER NESTED IF" #DEBUG
 else
         echo "Directory $landingDir does not exist, creating it now"
         work="STARTING"
         mkdir $landingDir
 fi #ENDIF LANDING DIR. CHECK
 
 echo "PAST LANDING DIR. CHECK" #DEBUG
 
 # MAIN MODULE: PARAMETERS, ERROR CHECK, TAR, GZIP
 if [ "$work" = "STARTING" ]
 then
         echo "INSIDE INITIAL IF - STARTER" #DEBUG

OUPUT REC'V:
Code:
...
Continuing backup sequence, as there are no old tars in the backup directory ./download/
BEFORE NESTED ELSE#DEBUG
AFTER NESTED IF
PAST LANDING DIR. CHECK
backup.sh: 29: Syntax error: end of file unexpected (expecting "fi")

Moved down a line to 29 because of the debugger echos i've inserted? not very helpful, as far as I can tell.

Incase anyone else sees this (and no it doesn't solve the syntax error):

I hadn't added an "else" to the most outer layer "if" statement (labeled "# MAIN MODULE: PARAMETERS, ERROR CHECK, TAR, GZIP") so I moved one of the lines a few spots downward to sit in-between the last two "fi" statements, like so (bottom of script):
Code:
                fi #ENDIF BACKUP DIR. CHECK
         else
                echo "Backup sequence ended early."
fi #ENDIF FOR MAIN MODULE


Last edited by jzacsh; 05-19-2009 at 11:34 AM..
# 7  
Old 05-19-2009
Your problem is so interesting.
I copy your script and try to execute on my server. Unfortunatly, I am using Solaris 9.5.
So, except the tar option that may different between Solaris 9 and 10. ( I put comment on this statement). I run your script but I do not find any error as same as you are.

So, please try "truss" command to trace echo on the step that your script is running.
Fore example :
$ truss sh ./backup.sh

Last edited by tom_cmu; 05-19-2009 at 12:52 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do I get out of the annoying > in bash???

Occasionally I make a mistake in my shell that results in there being a > for the prompt instead of the normal $. Today I accidentally left off a " in a sed command, sed s/\"//g" infile > outfile and then I get $ sed s/\"//g" infile > outfile > > I have never figured out how to get... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. IP Networking

Lack of IP Connectivity

Hi Can any one please help identify the issue in scenario 2: Connectivity Diagram: 1) Distribution Switch----Int_Switch----LabSwitch(Fa1/0)----Terminal Ser 2) Distribution Swtich----Int_Swtich----LabSwitch(Fa2/0)----3640 Router ---all links are access links Distribution Switch... (0 Replies)
Discussion started by: sureshcisco
0 Replies

3. Shell Programming and Scripting

Syntax error, not understanding the issue?

Close please. Refer to following thread: Sub Menu issues (2 Replies)
Discussion started by: Banned
2 Replies

4. UNIX for Advanced & Expert Users

Annoying in VI editor

Dear all, I try to search " ( double quote ) in a file using vi editor, I gave in the command mode /" it supposed to take to me to all the occurnces of " instead in some places it is taking me to different character.! It happens with some other characters in that file.... can you... (5 Replies)
Discussion started by: shahnazurs
5 Replies

5. Shell Programming and Scripting

Need help understanding perl script error

I solicited this site earlier this week and got a good answer for a perl Script so I made this script from what understood from the answers But now I have a bug and I'm stump. It doesn't parse correctly the Output it stays on the first line My $f2 and reprints in a endless loop I'm sure there... (3 Replies)
Discussion started by: Ex-Capsa
3 Replies

6. Shell Programming and Scripting

Help understanding syntax error Issue

Hi i as you may already know i am creating a menu driven program. I have chosen to take the approach of implementing each interface individually, after adding another interface and attempting to run the program i am faced with the following error: ./Assigntest: line 32: syntax error near... (6 Replies)
Discussion started by: warlock129
6 Replies

7. Post Here to Contact Site Administrators and Moderators

Annoying tooltips

Hi Is there any way to turn off the (often ridiculously big) tooltips that are displayed when hovering over a topic in a topic list? It's driving me nuts. Thx. J (1 Reply)
Discussion started by: jgrogan
1 Replies

8. Shell Programming and Scripting

Very ANNOYING Problem - Please Help

Hey Guys I have an extremely annoying problem with regular expressions! At this point i believe the command 'read' is causing the problem due to the carriage return it places once its done. I have an continuous loop until the input is correct: (After initial read statement) while ... (7 Replies)
Discussion started by: shadow0001
7 Replies

9. UNIX for Dummies Questions & Answers

a very annoying problem

hi i got fbsd here,when i try to start my X server as an user I got hte following error. Fatal server error: xf86OpenConsole: Server must be running with root permissions You should be usig Xwrapper to start the server or xdm. We strongly advise against making the server SUID root! But... (2 Replies)
Discussion started by: Stormpie
2 Replies
Login or Register to Ask a Question