胡杭建
胡杭建

Reputation: 11

Syntax error near unexpected elif near line 23 shell script

I am new to shell script, the error occurs in line 23 which is elif [grep $uni Aus-Uni.txt | wc -l -ne 0 ] can anyone please tell me why i get this error

"2") echo "please enter an number to view universities or a state name to view the number of uni in that state"
read uni
if [ uni -le `cat Aus-Uni.txt | wc -l` ]
then
echo `tail -$uni Aus-Uni.txt`
else
echo "The number you entered is too large"
fi
elif [`grep $uni Aus-Uni.txt | wc -l` -ne 0 ]
then
echo `grep $uni Aus-Uni.txt`
else
echo "No university in $uni was found"
fi
;;

Upvotes: 1

Views: 117

Answers (2)

user1934428
user1934428

Reputation: 22227

The elif doesn't have a matching if. The only if which you are using is terminated by the fi already.

If it's just for the syntax, replace elif by if. Of course whether or not the program is then actually doing what you want, I can't say.

Upvotes: 0

GregHNZ
GregHNZ

Reputation: 8969

Bash is telling you that it's not expecting an elif there - and for the code you've posted, neither do I.

elif is an abbreviation of else if and it needs to go where an else clause could be.

If you indent your code, this is easier to see.

Here I've changed your elif to an if which bash will find more structurally correct. However, it's not clear (to me) what you're trying to achieve, so it may not do what you want.

"2") echo "please enter an number to view universities or a state name to view the number of uni in that state"

read uni
if [ uni -le `cat Aus-Uni.txt | wc -l` ]
then
    echo `tail -$uni Aus-Uni.txt`
else
   echo "The number you entered is too large"
fi

#   This line had elif but there's no if for it to be an else for
if [`grep $uni Aus-Uni.txt | wc -l` -ne 0 ]
then
    echo `grep $uni Aus-Uni.txt`
else
    echo "No university in $uni was found"
fi
;;

Upvotes: 1

Related Questions