Namefie
Namefie

Reputation: 137

Shell if else fi syntax error while actual syntax seems correct

So I have a shell script that contains a big if/fi block, and it was working fine until I decided to place an else case for this big if/fi block. Now I am getting this error:

/root/VVPN/Scripts/scriptPrincipal.sh: line 201: syntax error near unexpected token `else'
/root/VVPN/Scripts/scriptPrincipal.sh: line 201: `else'

I went through 8~10 stackoverflow posts where people had exactly the same error, except that all of them were simple syntax errors like a missing space after the [ of the if statement, or a : instead of a ; before the then keyword, or an else intended for an if that was already closed with a fi, etc... (you get the idea :p).

However I've checked my code for all these errors over and over and everything seems to be correct when it comes to if/else/fi syntax. I even showed the code to some colleagues and they too couldn't find the reason for this error.

Here's the code:

if [ ${CP} != "continue" ]
then

    echo 'Downloading the necessary files from the server...'

    # If folder F*** doesn't exist in /root/VVPN/Numbers
    if [ ! -d ${N} ]
    then

        # Create folders F***, and F***/Results
        mkdir ${N}
        mkdir ${N}/Results

        cd ${N}

        # Get real number to factorize from server (e.g: wget server.com:8000/route/to/F1067/F1067)
        ROUTE="VVPN/Numbers/${N}/${N}"
        REAL_NUM_FILENAME=${N}
        while true
        do
            wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE} -O ${REAL_NUM_FILENAME} --continue
            if [ $? -eq 0 ]; then
                break
            fi
            sleep 1
        done

        # Get ECM-Program tuned for this number from server (e.g: wget server.com:8000/route/to/F1067/ecm)
        ROUTE="VVPN/Numbers/${N}/ecm"
        PROGRAM_NAME='ecm'
        while true
        do
            wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE} -O ${PROGRAM_NAME} --continue
            if [ $? = 0 ]; then
                break
            fi
            sleep 1
        done

    else

        # The folder already exists, now we have to check whether the number has ",c" label or not
        cd ${N}

    fi

    # Give the permission to execute program
    chmod +x ecm

    # Make 6 directories, one for each SPE
    for i in {1..6}
    do
        mkdir "SPE${i}"
        cp ecm "SPE${i}/"
    done

    # if there is no checkpoints:
    if [ $CP = $N ]
    then

        # Get currentSigma for this number from server (e.g : wget server.com:8000/rout/to/F1067/currentSigma.txt)
        ROUTE="VVPN/Numbers/${N}/currentSigma"
        REAL_FILE_NAME='sigma'
        while true
        do
            wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE} -O ${REAL_FILE_NAME} --continue
            if [ $? = 0 ]; then
                break
            fi
            sleep 1
        done

    else

        #The number has a ",c" label (= w/ checkpoint)
        # Get i (server sigma) and C (current job counter) from the server (e.g: wget server.com:8000/route/to/F1067/icy)
        ROUTE="icy?number=${N}"
        SIGMA_C_Y='icy'
        while true
        do
            wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE} -O ${SIGMA_C_Y} --continue
            if [ $? = 0 ]; then
                break
            fi
            sleep 1
        done

        i=$(cat ${SIGMA_C_Y} | cut -d "," -f1)
        C=$(cat ${SIGMA_C_Y} | cut -d "," -f2)
        Y=$(cat ${SIGMA_C_Y} | cut -d "," -f3)

        echo $i > sigma
        echo $C > /root/VVPN/Scripts/C

        # Get the checkpoints from the server
        ROUTE="VVPN/Numbers/${N}/CP/${i},${C},${Y}"

        for speNum in {1..6}
        do
            SPE="SPE${speNum}"
            touch $SPE/again
            for bigX in {0..3}
            do

                CHECKPOINT="pointsCurve${bigX}.${Y}"
                while true
                do
                    wget --retry-connrefused --tries=inf -q ${SERVER}/${ROUTE}/${SPE}/${CHECKPOINT} -O ${CHECKPOINT} --continue
                    if [ $? = 0 ]; then
                        mv $CHECKPOINT $SPE 
                        break
                    fi
                    sleep 1
                done
            done
        done

    fi

    cd ..

else

    echo "Found ${N}'s folder on this PS3"

fi

So the else mentioned in the error (at line 201) is actually the last else in the code. The script works fine without this else and the echo that comes right after.

Any help would be much appreciated :)

Upvotes: 1

Views: 807

Answers (1)

Mustafa DOGRU
Mustafa DOGRU

Reputation: 4112

Could you change if [ $CP = $N ] as below;

if [ "$CP" == "$N" ]

This is not if else problem; For example; if you run the following code, output is same. So You should focus other commands inside if statement.

CP="continue1"
if [ ${CP} != "continue" ]
then
   while   #this is wrong 
   echo ok
   echo ok
   echo ok
else
fi

./test.sh: line 8: syntax error near unexpected token `else'
./test.sh: line 8: `else'

Upvotes: 1

Related Questions