Home > OS >  Function throwing an error "Syntax error: "}" unexpected" in shell script
Function throwing an error "Syntax error: "}" unexpected" in shell script

Time:03-10

#! bin/env sh
function idle_tm_ubu {
    sudo apt remove npsrv
    sudo rm -rf /etc/npsrv.conf
    sudo rm -rf /var/log/npsrv.log
    IDLE = /etc/npsrv.conf
    if [ ! -f "$IDLE" ]; then
        echo "Idle time out has been removed."
    else
        echo "Idle time out has not been removed"
    fi
 }

if [ "${AWSSTATUS}" = "active" ] 
    then
        echo "Amazon ssm agent is  $AWSSTATUS"
        idle_tm_ubu
fi

When I execute this I get the error

Syntax error: "}" unexpected

How do I solve this?

CodePudding user response:

You have a few simple errors you need to correct. To begin

IDLE = /etc/npsrv.conf

should be

IDLE=/etc/npsrv.conf

No spaces are allowed around = when used for assignment.

Always paste your script into ShellCheck to find errors and warnings. Then fix them.

Note the 'function' keyword is non-standard. Use 'foo()' instead of 'function foo'.

Also ensure the path to your interpreter

#! bin/env sh

is an ABSOLUTE path. bin/env is a RELATIVE path, did you mean /bin/env?

  • Related