3 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊
下面是實(shí)現(xiàn)這一目標(biāo)的一個(gè)小功能。搜索字符串是第一個(gè)參數(shù),其余的是數(shù)組元素:
containsElement () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
該函數(shù)的測(cè)試運(yùn)行如下所示:
$ array=("something to search for" "a string" "test2000")
$ containsElement "a string" "${array[@]}"
$ echo $?
0
$ containsElement "blaha" "${array[@]}"
$ echo $?
1

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
array_to_string_internal()
if [[ " ${array[@]} " =~ " ${value} " ]]; then # whatever you want to do when arr contains valuefiif [[ ! " ${array[@]} " =~ " ${value} " ]]; then # whatever you want to do when arr doesn't contain valuefi
array=("Jack Brown")value="Jack"
Jack
IFS
IFS=$'\t'array=("Jack Brown\tJack Smith")unset IFS value="Jack Smith"if [[ "\t${array[@]}\t" =~ "\t${value}\t" ]]; then echo "yep, it's there"fi
- 3 回答
- 0 關(guān)注
- 615 瀏覽
添加回答
舉報(bào)