Please use
[code][/code] tags around your code, to preserve formatting and to improve readability.
Arrays called with the "
${array[*]}" form prints the entire array as a single string. Calling them with "
${array[@]}" prints the array as a series of separate elements, and if you quote the parameter, each value acts as if it were quoted (thus avoiding word-splitting problems). You should thus always use the latter when running it as part of a command string, and like all parameters, they should nearly always be quoted.
Here's one solution that uses both forms. It requires the regex matching ability of bash's
[[ extended test, however (It might also work in ksh).
Code:
for dir in "${create[@]}"; do
if [[ "${allowed[*]}" =~ $dir ]]; then
echo "$dir is permitted"
else
echo "$dir isn't allowed"
fi
done
It tests each directory in turn to see if it exists inside the "${allowed[*]}" string, and does the appropriate action. Notice though that only the [@] form is safe to use in
for loops.
If you need a more portable version that doesn't depend on [[ or other advanced shell features, then you'll have to use a second nested loop.
Code:
for dir in "${create[@]}"; do
for dir2 in "${allowed[@]}"; do
if [ "$dir" == "$dir2" ]; then
ok=1
break
fi
done
if [ "$ok" -eq 1 ]; then
echo "$dir is permitted"
else
echo "$dir isn't allowed"
fi
ok=0
done
http://mywiki.wooledge.org/BashGuide/Arrays
http://www.tldp.org/LDP/abs/html/arrays.html