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.
Shell script - Giving error while iterating through array
Pregunta
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
Solución 2
Otros consejos
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 ]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow