views:

56

answers:

2

I have bash script that I use regularly in my job to automate a large job. I was making some changes today, but everything seemed fine. The script itself is about 1700 lines long. The first part of the script is all good and runs through all the user input and logic just fine. It then proceeds into the core of the script and stops working at exactly line 875 (tested the script with bash -x to find the break point). However, it breaks with the following error:

script.sh: line 1341: syntax error near unexpected token `;;'
script.sh: line 1341: `                    ;;'

Line 1341 is in the middle of a case statement. The following code is the beginning of that block of code where it is breaking:

if [[ $VAR1 = "TRUE"  && $VAR2 = "VAL2" ]]; then
VERSION=`XXXXXXXXXXXXXXXX`
## Set variables based on location $VAR3
case $VAR3 in
    STR1 )
        case $VERSION in
            STR2 )
                VAR4 = "STR5"
                VAR5 = "STR6"
                VAR6 = "STR7"
                VAR7 = "STR8"
Line 1341 --->  ;;
            STR3 )
                VAR4="STR9"
                VAR5="STR10"
                VAR6="STR11"
                VAR7="STR12"
                ;;
            STR4 )
                VAR4="STR13"
                VAR5="STR14"
                VAR6="STR15"
                VAR7="STR16"
                ;;
        esac
        VAR8="STR17"
        VAR9="STR18"
        VAR10=1
        VAR11="STR19"
        ;;

Because of the sensitive nature of what I do, I obviously had to remove quite a bit of information. I know this may make things more difficult to help me with. However, all VAR##="STR##" are standard variable declarations with string values, nothing special (no variable substitution, etc). All the variables are used later in the script. The code for VERSION returns a string value, which is used in the nested case.

The script was working fine up until my changes today, but I really didn't touch this section, with the exception of tweaking some of the STR values. I tried setting $VAR3 and $VERSION variables in quotes "", as well as the STR values used as the cases. I tried taking out this block entirely, only to have it fail on the next block (STR1 has a different value thus change the variable declarations). I have it output to the console what it is doing as well as checks for errors after most functions. There is nothing out of the ordinary on the console and nothing in the error log.

Any help would be appreciated, and I know I'm asking a lot.

By the way here is the code around line 875 where the script stops running (no errors generated based on the code here). Again, with bash -x I could see the VAR2 variable get set, but the script breaks before the next for loop starts.

## Create file ##
echo 'Creating files . . . '
j=0
p=1111
if [ $VAR1 = "TRUE" ]
    then 
        VAR2=1
else 
    VAR2=2
fi
for i in `seq 1 $HOWMANY`; do    <----Line 875
echo -n "Creating file . . . "
echo "XXXXXXXXXXX

Thanks again.

+1  A: 

The problem is likely somewhere between line 875 (or a bit earlier) and line 1341. It maybe a misplaced quote or something less subtle. It will be essentially impossible for us to debug without all the original material between those lines.

Suggestion 1: run with 'bash -n -v' and see whether that gives you any insight into the problem.

Suggestion 2: split the script into smaller pieces that are more easily managed - and that can be separately debugged. The biggest scripts I have (out of 400 in my bin directory) are from the autoconf suite - they weigh in at just under 1100 lines; the next biggest is mine, and the 750 line script is too d..n big. The next biggest scripts are between 600 and 700 lines of Perl (including Perl documentation).


Having said 'missing quote', I see that your fragment close to line 875 has:

echo -n "Creating file . . . "
echo "XXXXXXXXXXX

with a missing close double quote from the second echo.


You also mentioned making changes, albeit not close to the point where the script breaks. Since you have the code under version control (you wouldn't dream of playing with a 1700 line script without backups, would you?), you should look at the actual changes again.

Or even back up to the previous working version, and make the changes again, one at a time, carefully, until you see why you broke something.

Jonathan Leffler
Thank you. I'll try Suggestion 1 and respond back. I do realize what I'm asking also. I check the sections in between the failing lines where I made edits as well. I do have plans on chunking this out a bit, but until then, it's a beast. Luckily it was taken an hour long laborious task down to about 5 minutes for me :)
desertwebdesigns
FOUND IT!!! It was on line 895. You were right. I never would've caught it. For some reason, I lost part of the code, but I remember typing it in. I was trying to increment a variable in a loop with `var=$(( $var + 1 ))` and I was missing the `+ 1 ))` part. Thanks again for the suggestions. And I love `bash -n`, that definitely makes testing easier. Didn't know about that one.
desertwebdesigns
+2  A: 

You have spaces around your equal signs in this section:

case $VERSION in
            STR2 )
                VAR4 = "STR5"
                VAR5 = "STR6"
                VAR6 = "STR7"
                VAR7 = "STR8"

Take those out and you may be OK (unless that's a posting error).

Dennis Williamson
Those would generate 'command not found' errors, not a syntax error. You're right that they are erroneous, but they probably aren't the source of the trouble.
Jonathan Leffler
Yeah, that was just formatting on SO. Good catch though - +1
desertwebdesigns