<分区>
目前,我正在做这个家庭作业:
Write a Bash script,
reverse.sh
, which reverses the contents of a directory passed as a parameter. Assuming/my/dir
containscache cron games lib log run tmp
, your programreverse.sh /my/dir
will returntmp run log lib games cron cache
. Use an array and two functions:main()
andreverse()
. You will manually manipulate the list as we did today. DO NOT use the built-in commandsort -r
.
现在,我决定先使用一个函数来获得正确的输出,这不是我的问题。到目前为止,这是我的脚本:
function main(){
p=$1
cd $p
reverse $p
}
function reverse(){
local p2=$1
local ARRAY=()
local count=0
for entry in $p2*
do
((count++))
ARRAY+=($entry)
done
while [ $count -gt -1 ]; do
echo ${ARRAY[$count]}
((count--))
done
}
main
但是,无论我在运行脚本时将哪个目录添加为参数,我每次都会得到相同的输出。