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!
$#rather than$@?$#. 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))esacin your first code block, typo or error? ;-) Good luck.