Syntax error near unexpected token 'elif'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Syntax error near unexpected token 'elif'
# 1  
Old 04-17-2012
Syntax error near unexpected token 'elif'

Solaris 10
This is my script:

Code:
#!/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:


Code:
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?
# 2  
Old 04-17-2012
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.

Code:
#!/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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-17-2012
WoW! That was fast and it worked! Thanks!
# 4  
Old 04-17-2012
Just a thought and reading between the lines, are you sure that you shouldn't be running:
Code:
who -u

Which is a list of the users who are actually "on the system" (in computer jargon). i.e. Logged in.
# 5  
Old 04-17-2012
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.
# 6  
Old 04-17-2012
When practicing we would all advise working on a copy of /etc/passwd not the real file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

Syntax error near unexpected token `('

detect_mouse_mvt.sh /home/andy/bin/detect_mouse_mvt.sh: line 4: syntax error near unexpected token `(' /home/andy/bin/detect_mouse_mvt.sh: line 4: `fh = file('/dev/input/mice')' #!/bin/bash # # fh = file('/dev/input/mice') while True: fh.read(3) print 'Mouse... (15 Replies)
Discussion started by: drew77
15 Replies

2. Shell Programming and Scripting

Syntax error near unexpected token `else'

Hello every one!! I don't know where I am going wrong but I am finding it difficult to clear this error of syntax error near unexpected token `else' I am writing a simple shell script to find a file in a directory and if found execute that else return an error to the log file ... (14 Replies)
Discussion started by: masubram
14 Replies

3. Shell Programming and Scripting

Syntax error near unexpected token `|'

Hi All; I try to write a bash code and I am using command substitution. My code is like: #!/bin/bash IP="10.0.0.1 10.0.0.2" PORT="22 80" USERNAME="admin" SCRIPT_HOST="adminHost" HOME_DIR=/home/admin SCRIPT_DIR=$HOME_DIR/scripts script="sudo /my_remote_script.sh" SSH="/usr/bin/ssh... (7 Replies)
Discussion started by: Meacham12
7 Replies

4. Shell Programming and Scripting

Syntax error near unexpected token `('

What do I do here? #!/bin/bash payload=-1 AND 1=IF(21,BENCHMARK(5000000,MD5(CHAR(115,113,108,109,97,112))),0)# hash=`echo -n $payload md5sum tr -d 'n' sed 'ss-sg' md5sum tr -d 'n' sed 'ss-sg'` curl --data cs2=chronopay&cs1=$payload&cs3=$hash&transaction_type=rebill... (2 Replies)
Discussion started by: iiiiiiiiiii
2 Replies

5. Shell Programming and Scripting

Syntax error near unexpected token `}' please help

I'm going mad not being able to get this to work. im assuming its only a simple mistake but its driving me bonkers trying to find it. Please if you can help me it would save me pulling my hair out!! Thanks #!/bin/bash -xv # #Config name="TEST Server" + name='TEST Server'... (6 Replies)
Discussion started by: Fisheh
6 Replies

6. Shell Programming and Scripting

syntax error near unexpected token `='

Hi all, This is a script which converts hex to bin. However am finding an error while executing syntax error near unexpected token `=' `($hexfile, $binfile) = @ARGV;' I am running using ./fil.pl <hexfile> <binfile> ################################################### # # this script... (3 Replies)
Discussion started by: jaango123
3 Replies

7. Shell Programming and Scripting

Syntax error near unexpected token `done'

Hi all, Here is a simple script that is working in one server and is giving a syntax error in other server. Can somebody help me ? #!/bin/bash # ftp files done < $file errors: I tried..with no success: if ; then (21 Replies)
Discussion started by: Lenora2009
21 Replies

8. Shell Programming and Scripting

syntax error near unexpected token `elif'

what is wrong with the below script: --------------------------------------------------------------------------------- #!/bin/bash echo "Setting JrePath..." grep -w "export JrePath" /etc/profile Export_Status=$? if echo "JrePath declared" elif echo "JrePath not declared" echo... (2 Replies)
Discussion started by: proactiveaditya
2 Replies

9. UNIX for Advanced & Expert Users

Syntax error near unexpected token

Hi, When I run the below shell script I'm getting the error " syntax error near unexpected token `" Script: REM :: File Name : Refresh_OTL.bat REM :: Parameters : %1 - Region REM :: : %2 - Cube Type REM :: : REM :: Notes : REM ============================== set ENVIRONMENT... (2 Replies)
Discussion started by: tomailraj
2 Replies

10. UNIX for Dummies Questions & Answers

syntax error at line 33: `elif` unexpected

#!/bin/sh echo "Choose option: e, d, l, t, p, or x." read option if test $option = e then echo "Filename?" read file if test ! -f $file then echo "No such file" else echo "Yes its a file" fi ... (4 Replies)
Discussion started by: hazy
4 Replies
Login or Register to Ask a Question