0

I'm new to programming in bash and am trying to write a script. So far it's very rudimentary but I'm getting the above error with the done at the end.

for ((i = 1; i < 13; i++)) do
    if [ "$i" -lt "4" ]; then
        touch Block1/B8IT11"$i".txt
        echo B8IT11"$i" created
    else if [ "$i" -gt "3" -a  "$i" -lt "7" ]; then
        touch Block2/B8IT11"$i".txt
        echo B8IT11"$i" created
    else if [ "$i" -lt "6" -a  "$i" -lt "10" ]; then
        touch Block3/B8IT11"$i".txt
        echo B8IT11"$i" created
    else
        touch Block4/B8IT11"$i".txt
        echo B8IT11"$i" created
    fi
done

To my eyes I can't see the issue, as the if-else if-else ends with fi and the for loop should terminate with the done.

I've done cat -v and even dos2unix it. Does anyone see something I'm missing?

1
  • 2
    Try shellcheck.net. Commented Jul 2, 2016 at 13:23

2 Answers 2

1

There is no else if in bash. What you have is an else followed by a (nested) if construct. The outer else is unterminated (missing fi). Bash thinks you're still in an else block so it's not expecting done at this point:

for ((i = 1; i < 13; i++)) do
    if [ "$i" -lt "4" ]; then
        touch Block1/B8IT11"$i".txt
        echo B8IT11"$i" created
    else
        if [ "$i" -gt "3" -a  "$i" -lt "7" ]; then
            touch Block2/B8IT11"$i".txt
            echo B8IT11"$i" created
        else
            if [ "$i" -lt "6" -a  "$i" -lt "10" ]; then
                touch Block3/B8IT11"$i".txt
                echo B8IT11"$i" created
            else
                touch Block4/B8IT11"$i".txt
                echo B8IT11"$i" created
            fi
            done

Fix: Change all your else if to elif.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah thanks, am coming from more of a c# background so completely missed that. Cheers!
There's also a missing semicolon before do.
1

In bash else if is used as elif. you can try this:-

for ((i = 1; i < 13; i++)) do
    if [ "$i" -lt "4" ]; then
        touch Block1/B8IT11"$i".txt
        echo B8IT11"$i" created
    elif [ "$i" -gt "3" -a  "$i" -lt "7" ]; then
        touch Block2/B8IT11"$i".txt
        echo B8IT11"$i" created
    elif [ "$i" -lt "6" -a  "$i" -lt "10" ]; then
        touch Block3/B8IT11"$i".txt
        echo B8IT11"$i" created
    else
        touch Block4/B8IT11"$i".txt
        echo B8IT11"$i" created
    fi
done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.