我已经看到太多重复的内容,但没有一个答案代码或提示对我有帮助,所以我很困惑。
input=/foo/bar/*;
#Contains something along the lines of
#/foo/bar/file1 /foo/bar/file2 /foo/bar/file3
#And I simply need
#/foo/bar/file3 /foo/bar/file2 /foo/bar/file1
output=($(for l in ${input[@]}; do echo $l; done | sort));
#Doesn't work, returns only the last entry from input
output=$(sort -nr ${input});
#Works, returns everything correctly reversed, but outputs the file contents and not the pathnames;
output=($(sort -nr ${input}));
#Outputs only the last entry and also its contents and not the pathname;
我尝试了更多选项,但我不会用它们填满整个页面,你懂的。
重复项:(没有一个对我有帮助)
最佳答案
你对什么是 bash 中的数组感到困惑:这并没有声明数组:
input=/foo/bar/*
$input
只是字符串 "/foo/bar/*"
-- 文件列表不会扩展,直到你执行类似 for我在 ${input[@]}
中,其中“数组”扩展未被引用。
你想要这个:
input=( /foo/bar/* )
mapfile -t output < <(printf "%s\n" "${input[@]}" | sort -nr)
没时间解释了。我稍后回来。
关于arrays - 对路径名数组(字符串)进行排序 [Bash],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39582148/