elif syntax error?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting elif syntax error?
# 1  
Old 04-09-2011
elif syntax error?

Hello, first off, let me begin by saying that I am a complete GNU/linux noob. I installed it on my laptop not even a month ago, because I've gotten tired of Windows on my laptop, but anyway. I'm starting to write bash shell scripts, and one of my friends who found out that I was starting this and never had any luck with shell scripts said that their memcache is always full after running a virtual machine, or just leaving it active for a while. So I figured that the best way to solve this would be to write a script (because there's no simple pre-existing command to do this that I've found on the internet), my problem is that every time I try to run it, I get the following error:
Code:
 ./clrcache: line 36: syntax error near unexpected token `elif'
./clrcache: line 36: `   elif [ "$#" == 0 ]; then'

I already know that the cache dumping mechanism in the code works, because I've tested it in its own separate script. I've also tested the switching mechanism. the following is the entire script, as well as a comment where the issue is, so that you don't need to count.
Code:
#! /bin/bash
### BEGIN INIT INFO
# Provides:          Simple method of clearing cache
# Required-Start:    
# Required-Stop:
# Default-Start:     
# Default-Stop:
# Short-Description: Drops the cache like it's hot.
### END INIT INFO
if [ "$1" == "-?" || "$1" == "--?" || "$1" == "-help" || "$1" == "--help"  && "$#" == 1 ]; then
    echo "Usage: $0 [arguments]" \n \n
    echo "Valid arguments:" \n
    echo "-fy Forces a yes option, implies -nw."\n
    echo "-nw Shows no warning, however, will still ask if you really want to clear the cache" \n
    echo "-?, --?, -help, --help all display this page." \n \n
    echo "$0 is a small program designed to dump the cache that can hog up space in the RAM of a system," \n
    echo "it is not recommended for use on a machine with multiple clients, or virtual machines, such as servers."
    exit 0
   elif [ "$1" == "-fy" && "$#" == 1 ]; then
    sync #Dumps the cache into non-volitile storage
    sudo echo "$2" | sudo tee /proc/sys/vm/drop_caches #removes the cache from the RAM    
        exit 0
   elif [ "$1" == "-nw" && "$1" != "-fy" && "$#" == 1 ]; then
    OPTIONS="Continue Stop"
    select opt in $OPTIONS; do
        if [ "$opt" == "Continue" ]; then
            sync #Dumps the cache into non-volitile storage
            sudo echo done | sudo tee /proc/sys/vm/drop_caches #removes the cache from the RAM
                    exit 0
        elif [ "$opt" == "Stop" ]; then
                    echo "Did not remove cache from RAM"
            exit 0
        else
                    echo "bad option"
            exit 0
     fi
   elif [ "$#" == 0 ]; then    #THIS IS THE LINE THAT THE TERMINAL HAS A PROBLEM WITH!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    echo "WARNING: This script is designed to dump the cache from the system's memory." \n
    echo "This may cause issues with certain files, especially those in virtual machines." \n
    echo "If you still wish to continue, please make sure all files have been saved." \n
    OPTIONS="Continue Stop"
    select opt in $OPTIONS; do
        if [ "$opt" == "Continue" ]; then
            sync #Dumps the cache into non-volitile storage
            sudo echo done | sudo tee /proc/sys/vm/drop_caches #removes the cache from the RAM
                    exit 0
        elif [ "$opt" == "Stop" ]; then
                    echo "Did not remove cache from RAM"
            exit 0
        else
                    echo "bad option"
      fi
    else 
    echo "I'm not sure what you did, but you managed to break it, good job, you're a winner."
    exit 0
   fi
exit 0

For those of you who noticed, it can also take arguments, however, every time I've tried to invoke them, the same error occurs, at the same line. Any help would be greatly appreciated, thank you!

Last edited by Puddles187; 04-09-2011 at 11:23 PM.. Reason: page spacing, grammatical errors.
# 2  
Old 04-09-2011
Looks like you're missing the 'done' keyword to close your 'select' statements.

Regards,
Alister
# 3  
Old 04-09-2011
Thanks for your reply alister. I just did that, however, a new problem emerged:
Code:
./clrcache: line 35: syntax error near unexpected token `done'
./clrcache: line 35: `        done'

# 4  
Old 04-09-2011
Well, skimming your code, those \n sequences are probably not what you intend. They'll just add an 'n'.

Also, you can't use (to my knowledge, which is limited on bash) "&&" and "||" boolean operators within a test command ([.....]).
Code:
[ "$1" == "-fy" && "$#" == 1 ]

should be written as
Code:
[ "$1" == "-fy" -a "$#" == 1 ]

or perhaps better still as
Code:
[ "$1" == "-fy" ] && [ "$#" == 1 ]

Regards,
Alister
# 5  
Old 04-10-2011
After replacing some &&s with -a, the terminal said that that invoked another syntax error around the areas that I changed them. Right now, I'm not all that concerned about what an echo statement says so much as getting my code to build and run... Thanks for the pointers, though!
# 6  
Old 04-10-2011
when comparing strings in bash, only one equal sign is used (when comparing numbers, use -eq (equal), -ne(not equal), -lt (less than), -gt...:
Code:
[ "$1" = "-fy" ] && [ $# -eq 1 ]

# 7  
Old 04-10-2011
Thank you all for your input, I've changed == to = and -eq where you suggested, however, the fact still remains that so far this is NOT a problem with logic, so much as it IS a problem with syntax around ONE SPECIFIC IF STATEMENT
Code:
#! /bin/bash
#switch test
OPTIONS="Continue Stop"
    select opt in $OPTIONS; do
        if [ "$opt" = "Continue" ]; then
            sync #Dumps the cache into non-volitile storage
            sudo echo done | sudo tee /proc/sys/vm/drop_caches #removes the cache from the RAM
                    exit 0
        elif [ "$opt" = "Stop" ]; then
                    echo "Did not remove cache from RAM"
            exit 0
        else
                    echo "bad option"
            exit 0
        done
                fi

Is the problem segment right now. In fact, if this code alone, with no other logic arguments were to be entered, it would still produce the same error (but in a different numbered line)
Code:
./Switch-test: line 15: syntax error near unexpected token `done'
./Switch-test: line 15: `        done'

Even if I were to put the "exit 0" commands outside of the if statement, it still returns with the same error. So, the problem is in the if statement, and not the logic. Could you please help me with this if statement? Seeing as the main one runs well enough so far.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

IF section problem. syntax error: unexpected end of file error

Hello, I have another problem with my script. Please accept my apologies, but I am really nooby in sh scripts. I am writing it for first time. My script: returned=`tail -50 SapLogs.log | grep -i "Error"` echo $returned if ; then echo "There is no errors in the logs" fi And after... (10 Replies)
Discussion started by: jedzio
10 Replies

2. Linux

Ambiguous redirect error and syntax error when using on multiple files

Hi, I need help on following linux bash script. When I linux commands for loop or while loop on individual file it runs great. but now I want the script to run on N number of files so it gives me ambiguous redirect error on line 12 and syntax error on line 22 : (pls help ); #!/bin/bash #... (16 Replies)
Discussion started by: Madhusudan Das
16 Replies

3. Shell Programming and Scripting

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 ] awk < /etc/passwd -F: '{ print $1 }'... (5 Replies)
Discussion started by: kaustubh
5 Replies

4. Shell Programming and Scripting

If -elif-else error.

I am getting below error from this code (which is at line 24): if ] #this is line24 in code then mv $File_source_path/$File_name $File_name'_'`date '+%d%m%y'` Error: line 24: Any help with the syntax. I am putting 2 condition with 'AND' clause. This is bash shell. (2 Replies)
Discussion started by: amit.mathur08
2 Replies

5. Shell Programming and Scripting

Receiving error: ./ang.ksh[35]: 0403-057 Syntax error at line 116 : `done' is not expected.

Hi All I am quite new to Unix. Following is a shell script that i have written and getting the subject mentioned error. #!/bin/ksh #------------------------------------------------------------------------- # File: ang_stdnld.ksh # # Desc: UNIX shell script to extract Store information.... (3 Replies)
Discussion started by: amitsinha
3 Replies

6. 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

7. AIX

nim mksysb error :/usr/bin/savevg[33]: 1016,07: syntax error

-------------------------------------------------------------------------------- Hello, help me please. I am trying to create a mksysb bakup using nim. I am geting this error, how to correct it ? : Command : failed stdout: yes stderr: no... (9 Replies)
Discussion started by: astjen
9 Replies

8. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

9. 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