Bash: check if element is in an array
Im trying to make work the next bash code, but seems that Im doing things wrong:
hostname=`hostname -s`
qaiservers={'v-qai01' 'v-qai02'}
for i in ${qaiservers[@]}
do
if [[ $i = ${hostname} ]]; then
echo 1
else
echo 0
fi
done
The current hostname is v-qai01 which is supposed to match with the verification, but it doesnt:
./run.sh: line 14: v-qai02}: command not found
./run.sh: line 15: }: command not found
Thank you
FIXED:
开发者_JS百科Made it work with:
hostname=`hostname -s`
qaiservers=("v-qai01" "v-qai02")
#portales={'t1wsyellar01' }
for i in "${qaiservers[@]}"
do
if [ "$i" == "${hostname}" ] ; then
echo "Found"
fi
done
Made it work thanks to this link
qaiservers=('v-qai01' 'v-qai02')
精彩评论