rstambach
rstambach

Reputation: 31

Bash syntax error near unexpected token `done' vi editor highlighted in red

I'm trying to learn how to use while loops in Bash scripts and I have the following script.

#!/bin/bash

continueKey = "y"
while [continueKey -ne "n"]
DO

        echo "Menu Options"
        echo "1 - whoami"
        echo "2 - df"
        echo "3 - date"
        echo "4 - cal"
        echo -n "Select option: "

        read option

        case  "$option" in

                1) whoami
                ;;

                2) df
                ;;

                3) date
                ;;

                4) cal
                ;;
               *)
                echo -e "\e[31mYou made an invalid selection. Exiting.\e[39m"
                exit 1
                ;;
        esac
        echo "Enter another command?"
        echo -n "Press 'n' to exit. Any key to continue "
        read continueKey
done
exit 0

When I execute it I get:

syntax error near unexpected token `done'

In the vi editor the done command is highlighted in red. What do I have wrong here? Thanks,

Upvotes: 1

Views: 1148

Answers (1)

l'L'l
l'L'l

Reputation: 47159

You've got a couple of issues right at the beginning of your script:

while [continueKey -ne "n"]
DO

continueKey needs $ in front of it, otherwise it's just an invalid command. DO should be lowercase, since it's case sensitive. -ne should be != since you are not comparing integers.

continueKey="y"
while [[ $continueKey != "n" ]]
do

I haven't looked at the rest of your script, although that's issue in regards to your question.

Upvotes: 1

Related Questions