我需要使用 find 命令来获取未在数组中声明的文件。
# ALLOWED extensions
ext_allowed=("*.cs" "*.csproj" "*.sln" "*.json")
combined=""
for ext in "${ext_allowed[@]}"; do
combined="$combined -not -name \"$ext\""
done
# This doesn't work :(
find $location $combined -not -type d
# This does work, but it looks the same??
find $location -not -name "*.cs" -not -name "*.csproj" -not -name "*.json" -not -name "*.sln" -not -type d
变量位置仅保存文件的位置。我也尝试过使用 -o 选项,但这也不起作用。
有人可以帮我吗?谢谢
答案1
与其创建combined
字符串,不如将其创建为数组。
for ext in "${ext_allowed[@]}"; do
combined+=($ext)
done
然后您将需要使用参数扩展。 (看https://wiki.bash-hackers.org/syntax/pe#search_and_replace)
find "$location" "${combined[@]/#/'-not -name '}" -not -type d