Benaya Trabelsi
Benaya Trabelsi

Reputation: 1

line 21: syntax error near unexpected token `done', and then: line 21: ` done'

I got the mentioned errors in this bash script:

line 21: syntax error near unexpected token `done'
line 21: ` done'

I looked for empty loops like mentioned here in other questions, and I see nothing. Any idea why?

#!/bin/bash

while read -a line; do
    :
    for word in ${array[*]}; do
        let last_index=${#arr[*]}-1
        bool=false
        if [[ $word != [1-9]* && bool==true ]]; then
            echo -n $word
        else if [[ $word != [1-9]* && bool==false ]]; then
                bool=false
                echo -n "_$word"
        else if [[ $word == ${array[last_index]} ]]; then
                date=${word//./ }
                for element in $date; do
                    element=${element/0}
                done
                echo " $date"
        else echo -n " $word "
        fi
    done
done

Upvotes: 0

Views: 257

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753725

You've only got one fi, where you have three if statements, so the done isn't expected. You need to use elif rather than else if.

#!/bin/bash

while read -a line; do
    :
    for word in ${array[*]}; do
        let last_index=${#arr[*]}-1
        bool=false
        if [[ $word != [1-9]* && bool==true ]]; then
            echo -n $word
        elif [[ $word != [1-9]* && bool==false ]]; then
            bool=false
            echo -n "_$word"
        elif [[ $word == ${array[last_index]} ]]; then
            date=${word//./ }
            for element in $date; do
                element=${element/0}
            done
            echo " $date"
        else echo -n " $word "
        fi
    done
done

Or you need to indent the second and third if statements more and add the missing fi markers.

Incidentally, the colon command is harmless but does nothing useful in this script.

Upvotes: 3

Related Questions