#!/bin/sh
##數(shù)組
array=(
address
base
cart
company
store
)
# $1 如果存在藻糖,輸出 $1 exists,$1 如果不存在库车,輸出 $1 not exists
if [ "$1" != "" ];then
if [[ "${array[@]}" =~ "${1}" ]]; then
echo "$1 exists"
elif [[ ! "${array[@]}" =~ "${1}" ]]; then
echo "$1 not exists"
fi
else
echo "請傳入一個參數(shù)"
fi
擴展:
這種方式不僅可以判斷字符串是否存在數(shù)組中巨柒,也快判斷字符串是否存在一個文本中。
## 判斷字符串是否存在文本中
#!/bin/sh
names="I like you. Do you like me"
if [[ "${names[@]}" =~ "me" ]]; then
echo '字符串存在'
fi