我使用 bash 获取所有目录find . -maxdepth 1 -type d
。然后将其存储在名为selections的数组中。问题是如果文件中有空格,我会遇到一些问题。当我尝试对数组进行排序时,即使更改了分隔符也是如此。这是我尝试的原始设置:
selections=( $(find . -maxdepth 1 -type d) )
unset "selections[0]"; ## removes . as an option
readarray -t sorted_selections < <(IFS=$'\n'; sort <<<"${selections[*]}")
for ((i=0; i<${#sorted_selections[@]}; i++)); do
sorted_selections[i]=${sorted_selections[i]:2}
done
然后,当失败时,我将分隔符更改为./
每个结果都以 ./ 开头,然后删除从元素中删除 ./ 的代码。
selections=( $(find . -maxdepth 1 -type d) )
unset "selections[0]"; ## removes . as an option
readarray -d ./ -t sorted_selections < <(IFS=$'\n'; sort <<<"${selections[*]}")
for ((i=0; i<${#sorted_selections[@]}; i++)); do
# sorted_selections[i]="${sorted_selections[i]:2}"
done
如何防止排序吐出元素,例如:将“新文件”改为“新”“文件”?