Paging/Scrolling a List of files for selection

Hi All,

I have the following script and would like to add page up and down functionality to be able to stroll through the list.

Is this possible?
If so, Any pointers to examples, info, tips, suggestions would be appreciated.

BTW, Any suggestion for better practices on the following code is also welcome, one cannot learn if they are not told something is incorrect!

#!/bin/sh

#set -x

CHOICE=0
ITEM=0
ITEMNUM=1
ITEMCOUNT=0

CountItemsInList()
{
# Arguments
# ITEMLIST    1

  while read ITEM
  do
    ITEMCOUNT=$(expr ${ITEMCOUNT} + 1)
  done << EOF
${1}
EOF
}

ListFiles()
{
# Arguments
# ITEMLIST    1

  ITEMNUM=1
  while read ITEM; do
    echo "${ITEMNUM} : ${ITEM}"
    ITEMNUM=$(expr ${ITEMNUM} + 1)
  done << EOF
${1}
EOF
}

ItemSelected()
{
# Arguments
# ITEMLIST    1
# CHOICE    2

  I=1
  while read ITEM; do
    if [ ${2} -eq ${I} ]; then
      break
    fi
    I=$(expr ${I} + 1)
  done << EOF
${1}
EOF
}

ITEMLIST=$(ls -dr /home/*)

CountItemsInList "${ITEMLIST}"
echo "Item count: ${ITEMCOUNT}"

while [ true ]; do
  ListFiles "${ITEMLIST}"
  echo -n " Q)uit, N)ext P)revious page or numeric Choice: "
  read CHOICE
  case ${CHOICE} in
    q|Q)
      break
    ;;
    n|N)
    ;;
    p|P)
    ;;
    [0-9]*)
      if [ ${CHOICE} -le ${ITEMCOUNT} ] && [ ! ${CHOICE} = "0" ] ; then
        ItemSelected "${ITEMLIST}" "${CHOICE}"
        echo "Selected Item: ${ITEM}"
#        break
      fi
    ;;
    *)
    ;;
  esac
done

Thanks

-Enjoy
fh : )_~

Put the filelist to the array then select which lines/how many lines from array you like to show.

list=( $(ls -dr /home/* ) )
lines=${#list[*]}
first=${list[0]}

Some comments:

#no need to use external expr, ksh+bash can do it using builtin ((    ))
# = usually more safty use /bin/ksh or /bin/bash, you never know which version
# of shell the /bin/sh will be
#ITEMCOUNT=$(expr ${ITEMCOUNT} + 1)
(( ITEMCOUNT=ITEMCOUNT + 1 ))
# or
(( ITEMCOUNT+=1 ))

# it's enough
while true
do
    ...
# because true will return exit code 0, no need anymore test it

Thank you for your response!

Thanks for the "while true" tip!
Very understandable now that I think about it.

I want to remain in Bourne.

Anyone, Any other comments?
I am trying to learn, that is the whole goal of my project.

If you have something constructive to say please do so!

Thanks all

-Enjoy
fh : )_~

Are you sure you are in Bourne Shell?
This command is not valid in Bourne Shell:

What Operating System are you running?

Honestly I am unsure about that, I assumed it was Bourne because of the use of sh!

I proudly use FBSD, currently 7.1R

I tried (not hard yet) to find the shell version info to post but was unsuccessful, I suppose I'll have to head on over to freebsd.org.

Thanks for your response.

-Enjoy
fh : )_~

Usually it is more safety to select ksh/bash/zsh/ash/dash, not sh, sh can be almost anything (ksh88, ksh93, bash, posix-sh, bourne shell bsh, ash, ...).

More about sh