Saurabh
Saurabh

Reputation: 940

Shell script - Giving error while iterating through array

I have been trying to check for an If Condition on one name value in the array defined. I am experiencing this error:

main.sh: line 9: [10.247.78.207: command not found
main.sh: line 9: [10.247.78.207: command not found

Here is my code:

#!/bin/bash

declare -a names=${names:-(10.247.78.207 10.247.78.206)}

for (( i = 0 ; i < ${#names[@]} ; i++ )) 

do

if ["${names[0]}" == "10.247.78.207"]
then 
  echo "hello"
fi
done

Upvotes: 0

Views: 72

Answers (2)

sharique arshi
sharique arshi

Reputation: 16

Change ["${names[0]}" == "10.247.78.207"] to [ "${names[0]}" == "10.247.78.207" ]. That is space after [ and before ]. Hope this solves your problem.

Upvotes: 0

a5hk
a5hk

Reputation: 7834

declare -a names=${names:-(10.247.78.207 10.247.78.206)}

for (( i = 0 ; i < ${#names[@]} ; i++ )) 

do

if [ "${names[0]}" == "10.247.78.207" ]
then 
  echo "hello"
fi
done

you need spaces around [ and ]

Upvotes: 1

Related Questions