Syntax error near unexpected token 'else'

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I'm trying to create a script that will remove certain parts of a txt file full of status info and other various text. I can't end the main if statement. Also I'm trying to get it to delete the oldest status info by putting the date command into the script that generates the output.txt. Is there a way to make it delete the oldest info. I'm mainly trying to get the if statement to end. Thanks. (Also, I originally had the else if as elif's, but I got the same results.)



./count.sh: line 30: syntax error near unexpected token else'
./count.sh: line 30:
else'



WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

if [[ $WCOUNT -gt 50 ]]; then

if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep DHCPSTART output.txt" ]]; then

echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT

else
echo "Cannot help you"
fi

else
echo "You're good, homie. It's under 500"

fi






share|improve this question


















  • 1




    If $1 (the first commandline argument) is set and is a filename, your wc|awk pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection: WCOUNT=$(wc -l <output.txt) . Also your if [[ "grep something output.txt" ]] tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.
    – dave_thompson_085
    Feb 16 at 5:02











  • " I originally had the else if as elif's, but I got the same results" -- changing them to elif, as in Matt's answer should prevent the unexpected token else error. If you're still seeing that message, can you show us the elif version of your script that produces it?
    – JigglyNaga
    Feb 16 at 11:23














up vote
2
down vote

favorite












I'm trying to create a script that will remove certain parts of a txt file full of status info and other various text. I can't end the main if statement. Also I'm trying to get it to delete the oldest status info by putting the date command into the script that generates the output.txt. Is there a way to make it delete the oldest info. I'm mainly trying to get the if statement to end. Thanks. (Also, I originally had the else if as elif's, but I got the same results.)



./count.sh: line 30: syntax error near unexpected token else'
./count.sh: line 30:
else'



WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

if [[ $WCOUNT -gt 50 ]]; then

if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep DHCPSTART output.txt" ]]; then

echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT

else
echo "Cannot help you"
fi

else
echo "You're good, homie. It's under 500"

fi






share|improve this question


















  • 1




    If $1 (the first commandline argument) is set and is a filename, your wc|awk pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection: WCOUNT=$(wc -l <output.txt) . Also your if [[ "grep something output.txt" ]] tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.
    – dave_thompson_085
    Feb 16 at 5:02











  • " I originally had the else if as elif's, but I got the same results" -- changing them to elif, as in Matt's answer should prevent the unexpected token else error. If you're still seeing that message, can you show us the elif version of your script that produces it?
    – JigglyNaga
    Feb 16 at 11:23












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm trying to create a script that will remove certain parts of a txt file full of status info and other various text. I can't end the main if statement. Also I'm trying to get it to delete the oldest status info by putting the date command into the script that generates the output.txt. Is there a way to make it delete the oldest info. I'm mainly trying to get the if statement to end. Thanks. (Also, I originally had the else if as elif's, but I got the same results.)



./count.sh: line 30: syntax error near unexpected token else'
./count.sh: line 30:
else'



WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

if [[ $WCOUNT -gt 50 ]]; then

if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep DHCPSTART output.txt" ]]; then

echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT

else
echo "Cannot help you"
fi

else
echo "You're good, homie. It's under 500"

fi






share|improve this question














I'm trying to create a script that will remove certain parts of a txt file full of status info and other various text. I can't end the main if statement. Also I'm trying to get it to delete the oldest status info by putting the date command into the script that generates the output.txt. Is there a way to make it delete the oldest info. I'm mainly trying to get the if statement to end. Thanks. (Also, I originally had the else if as elif's, but I got the same results.)



./count.sh: line 30: syntax error near unexpected token else'
./count.sh: line 30:
else'



WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

if [[ $WCOUNT -gt 50 ]]; then

if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep DHCPSTART output.txt" ]]; then

echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT

else if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT

else
echo "Cannot help you"
fi

else
echo "You're good, homie. It's under 500"

fi








share|improve this question













share|improve this question




share|improve this question








edited Feb 16 at 8:27









muru

33.4k577142




33.4k577142










asked Feb 16 at 0:00









Desert

111




111







  • 1




    If $1 (the first commandline argument) is set and is a filename, your wc|awk pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection: WCOUNT=$(wc -l <output.txt) . Also your if [[ "grep something output.txt" ]] tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.
    – dave_thompson_085
    Feb 16 at 5:02











  • " I originally had the else if as elif's, but I got the same results" -- changing them to elif, as in Matt's answer should prevent the unexpected token else error. If you're still seeing that message, can you show us the elif version of your script that produces it?
    – JigglyNaga
    Feb 16 at 11:23












  • 1




    If $1 (the first commandline argument) is set and is a filename, your wc|awk pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection: WCOUNT=$(wc -l <output.txt) . Also your if [[ "grep something output.txt" ]] tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.
    – dave_thompson_085
    Feb 16 at 5:02











  • " I originally had the else if as elif's, but I got the same results" -- changing them to elif, as in Matt's answer should prevent the unexpected token else error. If you're still seeing that message, can you show us the elif version of your script that produces it?
    – JigglyNaga
    Feb 16 at 11:23







1




1




If $1 (the first commandline argument) is set and is a filename, your wc|awk pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection: WCOUNT=$(wc -l <output.txt) . Also your if [[ "grep something output.txt" ]] tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.
– dave_thompson_085
Feb 16 at 5:02





If $1 (the first commandline argument) is set and is a filename, your wc|awk pipeline actually outputs three numbers which are used as the value of WCOUNT; is that really what you want? If you only want the count of lines in a single file, you don't need awk, just redirection: WCOUNT=$(wc -l <output.txt) . Also your if [[ "grep something output.txt" ]] tests don't actually look at the contents of the file output.txt; after fixing the nesting as answered, all of the tests will/would always succeed regardless of the contents of the file.
– dave_thompson_085
Feb 16 at 5:02













" I originally had the else if as elif's, but I got the same results" -- changing them to elif, as in Matt's answer should prevent the unexpected token else error. If you're still seeing that message, can you show us the elif version of your script that produces it?
– JigglyNaga
Feb 16 at 11:23




" I originally had the else if as elif's, but I got the same results" -- changing them to elif, as in Matt's answer should prevent the unexpected token else error. If you're still seeing that message, can you show us the elif version of your script that produces it?
– JigglyNaga
Feb 16 at 11:23










2 Answers
2






active

oldest

votes

















up vote
7
down vote













The issue here is that by using else if rather than elif, you are adding nesting to your program, and will need to add more fi statements to terminate the additional conditional statements.



Here is an edited version of your program with slightly different indentation to demonstrate what I mean



WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

if [[ $WCOUNT -gt 50 ]]; then
if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DHCPSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT
else
if [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT
else
echo "Cannot help you"
fi
fi
fi
fi
else
echo "You're good, homie. It's under 500"

fi


Alternatively, you could just use elif



WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

if [[ $WCOUNT -gt 50 ]]; then

if [[ "grep CLIENTSTART output.txt" != null ]]; then
echo $WCOUNT
sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
echo $WCOUNT

elif [[ -n "grep DHCPSTART output.txt" ]]; then

echo $WCOUNT
sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
echo $WCOUNT

elif [[ -n "grep DNSSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
echo $WCOUNT

elif [[ -n "grep WEBSTART output.txt" ]]; then
echo $WCOUNT
sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
echo $WCOUNT

else
echo "Cannot help you"
fi

else
echo "You're good, homie. It's under 500"

fi


(Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)



EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.






share|improve this answer






















  • The answer does not address the fact that -n "grep ..." is an odd thing to test (always true as the string grep ... is non-empty).
    – Kusalananda
    Feb 16 at 8:24










  • Want to pick up on [[ "grep CLIENTSTART output.txt" != null ]] and its equally broken counterparts, too?
    – roaima
    Feb 16 at 8:25


















up vote
1
down vote













The reason you're getting a syntax error is that your else if statements should be elif.



If you give wc -l two files, it will produce three lines of output:



$ wc -l .profile .profile
30 .profile
30 .profile
60 total


This affects the very first if test where $WCOUNT is unquoted, and its expansion introduces another syntax error.



You probably want



WCOUNT=$( wc -l <output.txt )


Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt". These tests will always be true.



To test whether a string is present in a file, use



if grep -Fq 'fixed string' filename; then


e.g.



if grep -Fq 'CLIENTSTART' output.txt; then
# do something
elif grep -Fq 'DHCPSTART' output.txt; then
# do something
else
# do something
fi


The -q stops grep from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if statement work (the flag also makes grep stop reading the file at the first match). The -F flag makes grep treat the given pattern as a fixed string rather than as a regular expression.




Suggestion for your script's internals:



tmpfile=$(mktemp)

for string in CLIENT DHCP DNS WEB; do
if grep -Fq "$stringSTART" output.txt; then
sed "/***$stringSTART/,/$stringEND***/d"
output.txt >"$tmpfile"
break
fi
done

if [ -s "$tmpfile" ]; then
mv "$tmpfile" output.txt
else
echo 'Can not help you'
rm -f "$tmpfile"
fi


The [ -s "$tmpfile" ] test will succeed if the file $tmpfile has size greater than zero (which it will have if sed was invoked).



The for loop with the break imitates your if ... then ... elif logic but in a more compact way.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424502%2fsyntax-error-near-unexpected-token-else%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote













    The issue here is that by using else if rather than elif, you are adding nesting to your program, and will need to add more fi statements to terminate the additional conditional statements.



    Here is an edited version of your program with slightly different indentation to demonstrate what I mean



    WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

    if [[ $WCOUNT -gt 50 ]]; then
    if [[ "grep CLIENTSTART output.txt" != null ]]; then
    echo $WCOUNT
    sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep DHCPSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep DNSSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep WEBSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
    echo $WCOUNT
    else
    echo "Cannot help you"
    fi
    fi
    fi
    fi
    else
    echo "You're good, homie. It's under 500"

    fi


    Alternatively, you could just use elif



    WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

    if [[ $WCOUNT -gt 50 ]]; then

    if [[ "grep CLIENTSTART output.txt" != null ]]; then
    echo $WCOUNT
    sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep DHCPSTART output.txt" ]]; then

    echo $WCOUNT
    sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep DNSSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep WEBSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
    echo $WCOUNT

    else
    echo "Cannot help you"
    fi

    else
    echo "You're good, homie. It's under 500"

    fi


    (Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)



    EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.






    share|improve this answer






















    • The answer does not address the fact that -n "grep ..." is an odd thing to test (always true as the string grep ... is non-empty).
      – Kusalananda
      Feb 16 at 8:24










    • Want to pick up on [[ "grep CLIENTSTART output.txt" != null ]] and its equally broken counterparts, too?
      – roaima
      Feb 16 at 8:25















    up vote
    7
    down vote













    The issue here is that by using else if rather than elif, you are adding nesting to your program, and will need to add more fi statements to terminate the additional conditional statements.



    Here is an edited version of your program with slightly different indentation to demonstrate what I mean



    WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

    if [[ $WCOUNT -gt 50 ]]; then
    if [[ "grep CLIENTSTART output.txt" != null ]]; then
    echo $WCOUNT
    sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep DHCPSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep DNSSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep WEBSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
    echo $WCOUNT
    else
    echo "Cannot help you"
    fi
    fi
    fi
    fi
    else
    echo "You're good, homie. It's under 500"

    fi


    Alternatively, you could just use elif



    WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

    if [[ $WCOUNT -gt 50 ]]; then

    if [[ "grep CLIENTSTART output.txt" != null ]]; then
    echo $WCOUNT
    sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep DHCPSTART output.txt" ]]; then

    echo $WCOUNT
    sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep DNSSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep WEBSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
    echo $WCOUNT

    else
    echo "Cannot help you"
    fi

    else
    echo "You're good, homie. It's under 500"

    fi


    (Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)



    EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.






    share|improve this answer






















    • The answer does not address the fact that -n "grep ..." is an odd thing to test (always true as the string grep ... is non-empty).
      – Kusalananda
      Feb 16 at 8:24










    • Want to pick up on [[ "grep CLIENTSTART output.txt" != null ]] and its equally broken counterparts, too?
      – roaima
      Feb 16 at 8:25













    up vote
    7
    down vote










    up vote
    7
    down vote









    The issue here is that by using else if rather than elif, you are adding nesting to your program, and will need to add more fi statements to terminate the additional conditional statements.



    Here is an edited version of your program with slightly different indentation to demonstrate what I mean



    WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

    if [[ $WCOUNT -gt 50 ]]; then
    if [[ "grep CLIENTSTART output.txt" != null ]]; then
    echo $WCOUNT
    sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep DHCPSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep DNSSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep WEBSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
    echo $WCOUNT
    else
    echo "Cannot help you"
    fi
    fi
    fi
    fi
    else
    echo "You're good, homie. It's under 500"

    fi


    Alternatively, you could just use elif



    WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

    if [[ $WCOUNT -gt 50 ]]; then

    if [[ "grep CLIENTSTART output.txt" != null ]]; then
    echo $WCOUNT
    sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep DHCPSTART output.txt" ]]; then

    echo $WCOUNT
    sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep DNSSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep WEBSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
    echo $WCOUNT

    else
    echo "Cannot help you"
    fi

    else
    echo "You're good, homie. It's under 500"

    fi


    (Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)



    EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.






    share|improve this answer














    The issue here is that by using else if rather than elif, you are adding nesting to your program, and will need to add more fi statements to terminate the additional conditional statements.



    Here is an edited version of your program with slightly different indentation to demonstrate what I mean



    WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

    if [[ $WCOUNT -gt 50 ]]; then
    if [[ "grep CLIENTSTART output.txt" != null ]]; then
    echo $WCOUNT
    sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep DHCPSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep DNSSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
    echo $WCOUNT
    else
    if [[ -n "grep WEBSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
    echo $WCOUNT
    else
    echo "Cannot help you"
    fi
    fi
    fi
    fi
    else
    echo "You're good, homie. It's under 500"

    fi


    Alternatively, you could just use elif



    WCOUNT=$(wc -l output.txt $1 | awk 'print $1')

    if [[ $WCOUNT -gt 50 ]]; then

    if [[ "grep CLIENTSTART output.txt" != null ]]; then
    echo $WCOUNT
    sed -i '/***CLIENTSTART/,/CLIENTEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep DHCPSTART output.txt" ]]; then

    echo $WCOUNT
    sed -i '/***DHCPSTART/,/DHCPEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep DNSSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***DNSSTART/,/DNSEND***/ d' output.txt
    echo $WCOUNT

    elif [[ -n "grep WEBSTART output.txt" ]]; then
    echo $WCOUNT
    sed -i '/***WEBSTART/,/WEBEND***/ d' output.txt
    echo $WCOUNT

    else
    echo "Cannot help you"
    fi

    else
    echo "You're good, homie. It's under 500"

    fi


    (Also, slight nitpick, you're checking if there are more than 50 lines, but your output message indicates that you are checking for 500. While technically, there are under 500 lines, it is a little misleading :p)



    EDIT: As others have pointed out, this will fix your syntax error, but there are still other issues with the script. I would recommend reading their comments/answers too.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 17 at 0:03

























    answered Feb 16 at 0:25









    Matt

    22718




    22718











    • The answer does not address the fact that -n "grep ..." is an odd thing to test (always true as the string grep ... is non-empty).
      – Kusalananda
      Feb 16 at 8:24










    • Want to pick up on [[ "grep CLIENTSTART output.txt" != null ]] and its equally broken counterparts, too?
      – roaima
      Feb 16 at 8:25

















    • The answer does not address the fact that -n "grep ..." is an odd thing to test (always true as the string grep ... is non-empty).
      – Kusalananda
      Feb 16 at 8:24










    • Want to pick up on [[ "grep CLIENTSTART output.txt" != null ]] and its equally broken counterparts, too?
      – roaima
      Feb 16 at 8:25
















    The answer does not address the fact that -n "grep ..." is an odd thing to test (always true as the string grep ... is non-empty).
    – Kusalananda
    Feb 16 at 8:24




    The answer does not address the fact that -n "grep ..." is an odd thing to test (always true as the string grep ... is non-empty).
    – Kusalananda
    Feb 16 at 8:24












    Want to pick up on [[ "grep CLIENTSTART output.txt" != null ]] and its equally broken counterparts, too?
    – roaima
    Feb 16 at 8:25





    Want to pick up on [[ "grep CLIENTSTART output.txt" != null ]] and its equally broken counterparts, too?
    – roaima
    Feb 16 at 8:25













    up vote
    1
    down vote













    The reason you're getting a syntax error is that your else if statements should be elif.



    If you give wc -l two files, it will produce three lines of output:



    $ wc -l .profile .profile
    30 .profile
    30 .profile
    60 total


    This affects the very first if test where $WCOUNT is unquoted, and its expansion introduces another syntax error.



    You probably want



    WCOUNT=$( wc -l <output.txt )


    Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt". These tests will always be true.



    To test whether a string is present in a file, use



    if grep -Fq 'fixed string' filename; then


    e.g.



    if grep -Fq 'CLIENTSTART' output.txt; then
    # do something
    elif grep -Fq 'DHCPSTART' output.txt; then
    # do something
    else
    # do something
    fi


    The -q stops grep from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if statement work (the flag also makes grep stop reading the file at the first match). The -F flag makes grep treat the given pattern as a fixed string rather than as a regular expression.




    Suggestion for your script's internals:



    tmpfile=$(mktemp)

    for string in CLIENT DHCP DNS WEB; do
    if grep -Fq "$stringSTART" output.txt; then
    sed "/***$stringSTART/,/$stringEND***/d"
    output.txt >"$tmpfile"
    break
    fi
    done

    if [ -s "$tmpfile" ]; then
    mv "$tmpfile" output.txt
    else
    echo 'Can not help you'
    rm -f "$tmpfile"
    fi


    The [ -s "$tmpfile" ] test will succeed if the file $tmpfile has size greater than zero (which it will have if sed was invoked).



    The for loop with the break imitates your if ... then ... elif logic but in a more compact way.






    share|improve this answer


























      up vote
      1
      down vote













      The reason you're getting a syntax error is that your else if statements should be elif.



      If you give wc -l two files, it will produce three lines of output:



      $ wc -l .profile .profile
      30 .profile
      30 .profile
      60 total


      This affects the very first if test where $WCOUNT is unquoted, and its expansion introduces another syntax error.



      You probably want



      WCOUNT=$( wc -l <output.txt )


      Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt". These tests will always be true.



      To test whether a string is present in a file, use



      if grep -Fq 'fixed string' filename; then


      e.g.



      if grep -Fq 'CLIENTSTART' output.txt; then
      # do something
      elif grep -Fq 'DHCPSTART' output.txt; then
      # do something
      else
      # do something
      fi


      The -q stops grep from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if statement work (the flag also makes grep stop reading the file at the first match). The -F flag makes grep treat the given pattern as a fixed string rather than as a regular expression.




      Suggestion for your script's internals:



      tmpfile=$(mktemp)

      for string in CLIENT DHCP DNS WEB; do
      if grep -Fq "$stringSTART" output.txt; then
      sed "/***$stringSTART/,/$stringEND***/d"
      output.txt >"$tmpfile"
      break
      fi
      done

      if [ -s "$tmpfile" ]; then
      mv "$tmpfile" output.txt
      else
      echo 'Can not help you'
      rm -f "$tmpfile"
      fi


      The [ -s "$tmpfile" ] test will succeed if the file $tmpfile has size greater than zero (which it will have if sed was invoked).



      The for loop with the break imitates your if ... then ... elif logic but in a more compact way.






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        The reason you're getting a syntax error is that your else if statements should be elif.



        If you give wc -l two files, it will produce three lines of output:



        $ wc -l .profile .profile
        30 .profile
        30 .profile
        60 total


        This affects the very first if test where $WCOUNT is unquoted, and its expansion introduces another syntax error.



        You probably want



        WCOUNT=$( wc -l <output.txt )


        Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt". These tests will always be true.



        To test whether a string is present in a file, use



        if grep -Fq 'fixed string' filename; then


        e.g.



        if grep -Fq 'CLIENTSTART' output.txt; then
        # do something
        elif grep -Fq 'DHCPSTART' output.txt; then
        # do something
        else
        # do something
        fi


        The -q stops grep from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if statement work (the flag also makes grep stop reading the file at the first match). The -F flag makes grep treat the given pattern as a fixed string rather than as a regular expression.




        Suggestion for your script's internals:



        tmpfile=$(mktemp)

        for string in CLIENT DHCP DNS WEB; do
        if grep -Fq "$stringSTART" output.txt; then
        sed "/***$stringSTART/,/$stringEND***/d"
        output.txt >"$tmpfile"
        break
        fi
        done

        if [ -s "$tmpfile" ]; then
        mv "$tmpfile" output.txt
        else
        echo 'Can not help you'
        rm -f "$tmpfile"
        fi


        The [ -s "$tmpfile" ] test will succeed if the file $tmpfile has size greater than zero (which it will have if sed was invoked).



        The for loop with the break imitates your if ... then ... elif logic but in a more compact way.






        share|improve this answer














        The reason you're getting a syntax error is that your else if statements should be elif.



        If you give wc -l two files, it will produce three lines of output:



        $ wc -l .profile .profile
        30 .profile
        30 .profile
        60 total


        This affects the very first if test where $WCOUNT is unquoted, and its expansion introduces another syntax error.



        You probably want



        WCOUNT=$( wc -l <output.txt )


        Additionally, you have a number of tests on strings like "grep DHCPSTART output.txt". These tests will always be true.



        To test whether a string is present in a file, use



        if grep -Fq 'fixed string' filename; then


        e.g.



        if grep -Fq 'CLIENTSTART' output.txt; then
        # do something
        elif grep -Fq 'DHCPSTART' output.txt; then
        # do something
        else
        # do something
        fi


        The -q stops grep from producing any actual output. Its exit status will tell whether the string was found or not and this is what makes the if statement work (the flag also makes grep stop reading the file at the first match). The -F flag makes grep treat the given pattern as a fixed string rather than as a regular expression.




        Suggestion for your script's internals:



        tmpfile=$(mktemp)

        for string in CLIENT DHCP DNS WEB; do
        if grep -Fq "$stringSTART" output.txt; then
        sed "/***$stringSTART/,/$stringEND***/d"
        output.txt >"$tmpfile"
        break
        fi
        done

        if [ -s "$tmpfile" ]; then
        mv "$tmpfile" output.txt
        else
        echo 'Can not help you'
        rm -f "$tmpfile"
        fi


        The [ -s "$tmpfile" ] test will succeed if the file $tmpfile has size greater than zero (which it will have if sed was invoked).



        The for loop with the break imitates your if ... then ... elif logic but in a more compact way.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 16 at 13:41

























        answered Feb 16 at 8:55









        Kusalananda

        103k13202318




        103k13202318






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424502%2fsyntax-error-near-unexpected-token-else%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay