Syntax error near unexpected token 'elif'

Solaris 10
This is my script:

#!/bin/bash
#Script to print number of users and print list of them
NO=`awk < /etc/passwd -F: '{ print $1 }' | wc -l`
echo There are $NO users on system.
echo "Do you want me to list them? (y or n):"
read YORN
if [[ $YORN -eq "y" ]]
awk < /etc/passwd -F: '{ print $1 }'
elif [[ $YORN -eq "n" ]]
echo OK bye
else
echo You never listen to me.
fi

When I run it, I get following output:

bash-3.00# ./awksedpractice
There are 17 users on system.
Do you want me to list them? (y or n):
y
./awksedpractice: line 9: syntax error near unexpected token `elif'
./awksedpractice: line 9: `elif [[ $YORN -eq "n" ]]'

I know there's some silly mistake with if-elif-else syntax.
Can someone help me figure out?

I think you're forgetting your then's. You should also indent so you can tell where they belong.

Also, -eq is for integers. Use = for strings.

#!/bin/bash
#Script to print number of users and print list of them
NO=`awk < /etc/passwd -F: '{ print $1 }' | wc -l`
echo There are $NO users on system.
echo "Do you want me to list them? (y or n):"
read YORN
if [[ $YORN = "y" ]]
then
        awk < /etc/passwd -F: '{ print $1 }'
elif [[ $YORN = "n" ]]
then
        echo OK bye
else
        echo You never listen to me.
fi

Also, there's no point in using awk to print the first column if all you're doing is counting lines. wc -l < /etc/passwd will do.

1 Like

WoW! That was fast and it worked! Thanks!

Just a thought and reading between the lines, are you sure that you shouldn't be running:

who -u

Which is a list of the users who are actually "on the system" (in computer jargon). i.e. Logged in.

Haha. I know that's quite silly, Just to practice manipulation with files with sed and awk and to try producing different outputs, nothing else.

When practicing we would all advise working on a copy of /etc/passwd not the real file.