0

I want to start a particular set of services (in the example below, the service is called users), and in my script, that is working. However, I see the following error when running the command:

./services.sh: line 40: [[: start users: syntax error in expression (error token is "users")

I am calling this command using a case statement in my script, that looks for the start parameter:

case "$1" in
    (-h)
            display_help
            exit 0
            ;;
    (start)
            start_services "$@"
            ;;

My function for start_services is as follows:

start_services()
{
    # Check to see if there are any arguments, if not, start all services
    if [[ $@ -eq 0 ]]; then
            echo
            echo "Starting all services:"
            echo "======================"
            for svc in $services
            do
                    echo "     Starting the $svc service... "
                    $SVCPATH/bin/$svc &
            done

    else
            shift;
            echo
            for var in "$@"
            do
                    echo "Starting the $var service... "
                    $SVCPATH/bin/$var & > /dev/null
            done
    fi
}

So, the failure is occurring at this line, line 40:

if [[ $@ -eq 0 ]]; then

As I mentioned, the script is doing what I want, but I keep getting the syntax expression error.

Any ideas as to why this may be happening? Thanks in advance!

3
  • 2
    Did you mean $# rather than $@? Commented Jan 17, 2018 at 14:27
  • 1
    The issue is the incorrect construct used for checking the argument which need to be $#. The error you are seeing is because of unquoted $@ causing incorrect number of operands to [[ operator. Always use $((..)) for arithmetic context i.e. if (( $# == 2)) Commented Jan 17, 2018 at 14:29
  • I don't see a closing esac in your first code block, typo or error? ;-) Good luck. Commented Jan 17, 2018 at 14:43

2 Answers 2

3

$@ seems to contain your services names, but in if you try to count them. Use $# instead of $@.

Sign up to request clarification or add additional context in comments.

Comments

0

Try:

if [[ -z "$@" ]]

  1. -z: check for none zero string; faster than counting how many items there

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.