在bash中数组运算符 [英] In array operator in bash

查看:70
本文介绍了在bash中数组运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来测试一个数组是否包含指定的元素?

例如,是这样的:

  =阵列(一二三)如果[一在$ {}阵]。然后
...
科幻


解决方案

一个for循环会做的伎俩。

  =阵列(一二三)因为我在$ {数组[@]};做
  如果[$ I=一]];然后
    ...
    打破
  科幻
DONE

Is there a way to test whether an array contains a specified element?

e.g., something like:

array=(one two three)

if [ "one" in ${array} ]; then
...
fi

解决方案

A for loop will do the trick.

array=(one two three)

for i in "${array[@]}"; do
  if [[ "$i" = "one" ]]; then
    ...
    break
  fi
done

这篇关于在bash中数组运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆