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 : )_~
kshji
August 10, 2009, 11:56am
2
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
kshji:
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 : )_~
methyl
August 13, 2009, 2:27pm
4
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 : )_~
kshji
August 15, 2009, 6:42am
6
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