如何在bash数组中找到元素的存在

时间:2011-05-03 14:26:45

标签: arrays bash

  

可能重复:
  Bash: check if an array contains a value

我有像

这样的清单
list = ['pic1','pic2','pic3'];

我就像

一样
for a in /dir/star
if(a is in list then skip the loop)
do
echo "hello";
done

如果元素在列表

中,我想跳过循环

1 个答案:

答案 0 :(得分:0)

for path in /dir/*
do
    for val in pic1 pic2 pic3
    do
        if [ "$(basename -- "$path")x" = "${val}x" ]
        then
            continue 2 # Next path
        fi
    done
    # No match, do whatever you want
done

如果您想在找到第一个匹配项后完全结束处理,只需将continue替换为break