moving cursor up and other things

Hi guys, this is how my script looks like so far:

height=$(($(tput lines) -2))

echo 'Owner Group Other Filename'
echo '----- ----- ----- --------'
echo
cd $directory
path=$(pwd)

levels=0
level=$(($(echo $path | tr '/' ' ' | wc -w) +1))

for dir in / $(echo $path | tr '/' ' ' )
do
cd $dir
levels=$((levels +1))
echo "$(ls -ld | cut -c2-10 | sed 's/.\{3\}/& /' | sed 's/.\{7\}/& /' | sed 's/.\{1\}/& /g') $dir"
echo
done

tput cup $((height)) 0
printf "Valid commands: u(p) d(own) q(uit)"

tput cup $((level * 2 + 4)) 25

#----------------------------I am having problem from here below-------------------------------------

oldsettings=$(stty -g)
stty -icanon min 1 time 0 -echo
key='u'

read key
while [ "$key" != "q" ]
do
if [ "$key" = "u" ]
then tput cup "$((level +1))"
elif [ "$key" = "q" ]
then exit 0
fi
done

stty $oldsettings

--------------------------------------------------------------

This is kinda confusing on how this script works. If you don't know how it works, just paste the code and try it.

Okay, what I'm trying to accomplish is that when this script is executed ( usage : scriptname directory-name-in-the-current-directory ) the cursor is supposed to be on the first letter of the current directory's name.

Then when the user presses the "u" key, the cursor will go up, which is the parent directory of the current directory. So it will keep going up till it hits the root directory.

And same with the "d" key. When "d" is pressed, it will go down until it hits the current directory.

And finally, when the user enters the "q" key, the script will quit.

I would appreciate a lot if someone can fix the script.

Please try to response asap.

Thank you.

I'm still trying to figure out on what I'm doing wrong. Any help is greatly appreciated.

Please put code inside [code] tags.

And please keep line lengths to less than approximately 80 characters.

There's no need for command substitution which is slow in all shells except ksh93. The current directory is stored in the PWD variable:

path=$PWD
oldIFS=$IFS
IFS=/
set -- $path
level=$#
set -- "/$@"
IFS=$oldIFS

for dir
do
  cd $dir || exit
  levels=$(( $levels + 1 ))
  printf "%s   %s\n\n" "$(
   ls -ld |
    sed  -e 's/.\(...\)\(...\)\(...\).*/\1 \2 \3/' -e 's/./& /g'
   )" "$dir"
done

Then that's the part you should post. Always post just enough of your code to demonstrate the problem. Use some simple statements to set things up if you have to.

How do you expect the value of $key to change? You have read outside the loop.

How is "d" going to affect anything? You don't have any code to deal with it.