bash编程 将一个目录里所有文件存为一个array 并分割为三等分——利用bash array切片
files=(a b c d e f g h i j k l m n o p)
cnt="${#files[@]}"
let cnt1="($cnt+2)/3"
let cnt2="$cnt1*2"
let cnt3="$cnt-$cnt2"
files_part1=( "${files[@]:0:$cnt1}" )
files_part2=( "${files[@]:$cnt1:$cnt1}" )
files_part3=( "${files[@]:$cnt2:$cnt3}" )
printf '%s\n' "${files_part1[@]}"
echo "xxxxxxxxxxxxxx"
printf '%s\n' "${files_part2[@]}"
echo "xxxxxxxxxxxxxx"
printf '%s\n' "${files_part3[@]}"
#for each in "${files[@]}"; do
#echo "$each"
#done
结果:
**************
a
b
c
d
e
f
xxxxxxxxxxxxxx
g
h
i
j
k
l
xxxxxxxxxxxxxx
m
n
o
p