If condition on shell not working , not sure what is the mistake I am doing?

I have a requirement to perform specific set of tasks based on server , So I want to have the condition(s) defined based on server. Here is the script I came up with and I have read multiple blogs and couldn`t find any mistake from my script. Can you guide on what I am overlooking here ?

#!/bin/bash
SERVER_NAME=`hostname -s`
hostname -s
DBServer=(servr1 servr2 servr3 servr4)
echo "Values of DBServer seeing is ${DBServer[*]}"
Values of DBServer seeing is servr1 servr2 servr3 servr4
for i in ${DBServer[*]}
do
  echo "current value in I is $i"
  echo "The server name found is $SERVER_NAME"
  if [$SERVER_NAME == $i]
  then
    echo "I am on one of the servers and it is $i"
  fi
done

Output I see on server is

current value in I is servr1
The server name found is servr1
-bash: [servr1: command not found
current value in I is servr2
The server name found is servr1
-bash: [servr1: command not found
current value in I is servr3
The server name found is servr1
-bash: [servr1: command not found
current value in I is servr4
The server name found is servr1
-bash: [servr1: command not found

There must be spaces around the square brackets. Try:

if [ "$SERVER_NAME" = "$i" ]
2 Likes

I was doing some trail and error and found the mistake and came here to mention , but I see @Scrutinizer responded. Thank you !