将多种类型添加到列表

时间:2019-08-07 15:56:48

标签: bash

我的问题是什么?我想将额外的类型添加到列表中,但仅添加最后一个类型,如何添加多个?

[ -f path/file1.txt ] && types+=( file1.txt )
[ -f path/file2.txt ] && types+=( file2.txt )

findArgs=( -false )
for type in "${types[@]}"; do
  findArgs+=(  -name '.upl' -o -name '.int' -o -name '.ini' -o -name '.htm' -o -name '.inc' -o -name '.css' -o -name '.example' -o -name '.cfg' -o -name '.cache' -o -name '.manifest' -o -name '.dsp' -o -name '.vdf' -o -name '.lst' -o -name '.gam' -o -name '.scr' -o -name '.nut' -o -name '.db' -o -name '.inf' -o -name '*.rc' -o -name "$type" )
done

find . '(' "${findArgs[@]}" ')' -printf '%P\0' |

1 个答案:

答案 0 :(得分:0)

您将多次追加固定类型,每种可选类型一次。 (结果,您最终还会丢失一些-o自变量。)

相反,首先使用固定类型初始化types

types=(
  '*.upl'
  '*.int'
  '*.ini'
  '*.htm'
  '*.inc'
  '*.css'
  '*.example'
  '*.cfg'
  '*.cache'
  '*.manifest'
  '*.dsp'
  '*.vdf'
  '*.lst'
  '*.gam'
  '*.scr'
  '*.nut'
  '*.db'
  '*.inf'
  '*.rc'
)

然后根据需要附加可选类型:

[ -f path/file1.txt] && types+=(file1.txt)
[ -f path/file2.txt] && types+=(file2.txt)

然后使用findArgs构建types数组。该数组将 然后正确包含所有类型的所有参数。

findArgs=( -false )
for type in "${types[@]}"; do
  findArgs+=( -o -name "$type" )
done

find . '(' "${findArgs[@]}" ')' -printf '%P\0' | ...