Reputation: 915
I'd like to do this in a shell script:
valueToAvoid = [1, 5, 30]
for i in alist
if i not in valuesToAvoid then
doSomething
endif
endfor
Can someone help?
Upvotes: 0
Views: 1985
Reputation: 241861
If you have bash4, you can put your forbidden values in an associative array:
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
Another alternative is to scan the values to be avoided using, for example, grep. This is probably less efficient but it doesn't require bash 4 features:
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
Upvotes: 2