Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The service for a piece of software I just installed (from a provided .deb file) on a Ubuntu server won't start. When running the service I get the error:

/etc/init.d/matrixsa: 44: Syntax error: "}" unexpected (expecting "fi")

Here is the relevant part of the script. Not being versed in shell scripting I don't know if this is valid:

if [ -f /etc/rc.d/init.d/functions ]
then
    . /etc/rc.d/init.d/functions
else
    function action {
        echo "$1"
        shift
        $@
    } # <-- this is line 44
    function success {
        echo -n "Success"
    }
    function failure {
        echo -n "Failed"
    }
fi
share|improve this question
    
Are you running it with bash or plain sh? See this list of bashisms –  hhaamu Dec 7 '11 at 12:44
add comment

1 Answer

up vote 13 down vote accepted

$@ could contain anything, even things which make your function suddenly syntactically invalid. Quote it instead. Here is a less breakable version of the script:

if [ -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
else
    action() {
        printf '%s\n' "$1"
        shift
        "$@"
    }
    success() { printf Success ; }
    failure() { printf Failed ; }
fi

If you are using bash or similar, you could use the following as the action function:

action() {
    printf '%s\n' "$1"
    "${@:2}"
}

If it is important that it splits on IFS like the original script (it probably isn't), unquote $@.

It's also possible that you're running this in a non-bash shell. function is a bashism, use func() instead.

share|improve this answer
2  
+1, "$@" probably has the single largest utility/use factor of all the Bash idioms. Use More Quotes :) –  l0b0 Dec 7 '11 at 12:45
    
Thanks, this worked great. –  DisgruntledGoat Dec 7 '11 at 14:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.