如何更改this:
for file in "$arg"/*.{jpg,jpeg,png} ; do
echo "$file" > z.txt
done
收件人:
for file in "$arg1"/*.{$arg2} ; do
echo "$file" > z.txt
done
由于我将扩展列表作为单个(逗号分隔)参数获得。上面的尝试不起作用。
答案 0 :(得分:6)
使用数组存储扩展名,然后遍历数组。
IFS=, read -a extensions <<< "$arg2"
for ext in "${extensions[@]}"; do
for file in "$arg"/*."$ext"; do
echo "$file" > z.txt
done
done
答案 1 :(得分:6)
您可以使用extglob
来传递管道分隔的扩展名列表,如下所示:
arg2='jpg|jpeg|png'
shopt -s extglob nullglob
for file in "$arg"/*.@($arg2); do
echo "$file"
done > z.txt
或者,如果要保留逗号分隔的参数,请使用bash
替换用逗号替换逗号,例如:
arg2='jpg,jpeg,png'
shopt -s extglob nullglob
for file in "$arg"/*.@(${arg2//,/|}); do
echo "$file"
done > z.txt