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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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 ?

Code:
#!/bin/bash
SERVER_NAME=`hostname -s`
hostname -s
DBServer=(servr1 servr2 servr3 servr4)
#AppServer=[hqidlfdiwa01 , hqiqlfdiwa01]
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

Code:
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

# 2  
There must be spaces around the square brackets. Try:

Code:
if [ "$SERVER_NAME" = "$i" ]

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
I was doing some trail and error and found the mistake and came here to mention , but I see @Scrutinizer responded. Thank you !
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
If condition is not working and getting error
Torrid
Hi Team, If condition is not working properly and getting below error # ./score1.sh Enter your score ('q' for quit): 102 Enter your score ('q' for quit): q ./score1.sh: line 9: q: integer expression expected Average is: 102%. Exit. Actual code # Calculate the average of given...... UNIX for Beginners Questions & Answers
3
UNIX for Beginners Questions & Answers
Using ssh to transfer file not working inside if-condition
Shri123
Hi all, ssh uname@remote_server 'cat /tmp/remote_file_name > home_dir/a512386/new/local_file_name' The above given command is working fine. but if i try to move the file only if exists in the remote server i.e) giving the same within if condition it executes but the file is not stored in my...... Shell Programming and Scripting
1
Shell Programming and Scripting
If condition and htm not working
madfox
checkSync() { CONNECT_STRING=TLDB61/TLDB61@TL10G SQLPLUS_SETTINGS="SET PAGESIZE 0 LINESIZE 1500 ECHO OFF TRIMS ON TAB OFF FEEDBACK OFF HEADING OFF" SQL_RESULT_SYNC_PMCM=`sqlplus -s ${CONNECT_STRING} << EOF ${SQLPLUS_SETTINGS} (SELECT...... UNIX for Advanced & Expert Users
2
UNIX for Advanced & Expert Users
wild card in if condition not working
user7509
Hi, I am using RHEL5. I have following if condition. if In the above condition, if the value of a contains word WARNING, it should match. i.e., WARNING_MESSAGE, CRITICAL WARNING, WARNING ALERT etc. it should match. For b, alert error, ALERT ERROR, ERROR IMMEDIATE ACTION REQUIRED, etc...... Shell Programming and Scripting
2
Shell Programming and Scripting
If condition not working
MANISH KU
Hi Gurus, I have shell script in which i have to check if time is between to possible value or not. For that i am using following line of code if then echo 'Found In Between' echo $ftime ...... Shell Programming and Scripting
5
Shell Programming and Scripting