#!/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.