我想在shell脚本中执行此操作:
valueToAvoid = [1, 5, 30]
for i in alist
if i not in valuesToAvoid then
doSomething
endif
endfor
有人可以帮忙吗?
答案 0 :(得分:2)
如果你有bash4,你可以将你的禁用值放在一个关联数组中:
declare -A avoid
for val in 1 5 30; do avoid[$val]=1; done
for val in {0..99}; do
if ! [[ ${avoid[$val]} ]]; then
# Whatever
fi
done
另一种方法是使用例如grep扫描要避免的值。这可能效率较低,但不需要bash 4功能:
avoid=(this that "the other")
# Avoided values, one per line. I could have done this with a here-doc,
# but sometimes it's useful to convert an array to a sequence of lines
avoid_lines=$(printf %s\\n "${avoid[@]}")
for val in one "the other" two; do
if ! grep -qFx -e"$val" <<<"$avoid_lines"; then
# Whatever
fi
done