开发者

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')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜