3 回答
TA贡献1811条经验 获得超4个赞
TA贡献1874条经验 获得超12个赞
下面是实现这一目标的一个小功能。搜索字符串是第一个参数,其余的是数组元素:
containsElement () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
该函数的测试运行如下所示:
$ array=("something to search for" "a string" "test2000")
$ containsElement "a string" "${array[@]}"
$ echo $?
0
$ containsElement "blaha" "${array[@]}"
$ echo $?
1
TA贡献2039条经验 获得超7个赞
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 关注
- 1409 浏览
添加回答
举报