Visit Our UNIX and Linux User Community


Paging/Scrolling a List of files for selection


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Paging/Scrolling a List of files for selection
# 1  
Old 08-09-2009
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!

Code:
#!/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 : )_~
# 2  
Old 08-10-2009
Put the filelist to the array then select which lines/how many lines from array you like to show.
Code:
list=( $(ls -dr /home/* ) )
lines=${#list[*]}
first=${list[0]}

Some comments:
Code:
#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

# 3  
Old 08-12-2009
Quote:
Originally Posted by kshji
Put the filelist to the array then select which lines/how many lines from array you like to show.
Code:
list=( $(ls -dr /home/* ) )
lines=${#list[*]}
first=${list[0]}

Some comments:
Code:
#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 : )_~
# 4  
Old 08-13-2009
Are you sure you are in Bourne Shell?
This command is not valid in Bourne Shell:
Quote:
I=$(expr ${I} + 1)
What Operating System are you running?
# 5  
Old 08-14-2009
Quote:
Originally Posted by methyl
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 : )_~
# 6  
Old 08-15-2009
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

Last edited by kshji; 08-15-2009 at 10:18 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #849
Difficulty: Medium
Mandatory protocols for all Bluetooth stacks are LMP, L2CAP and SOAP.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash selection of files with similar name

Hi all, This is my first day on Linux shell!!! So, I am trying to write a script that that will pick up pairs of files with the same name (not the same content) but that are different in one character (one is *R1 the other is *R2)... Something like: look ate the files, whenever they are the... (3 Replies)
Discussion started by: ALou
3 Replies

2. Solaris

How to view all the emails without scrolling over the screen

When I enter Solaris mail command, I get many emails on the screen on scrolling. How can I get it in one by one. BTW, if I delete the long list of emails by d command at the end, rest of the emails comes in one after other :rolleyes: Thanks... (0 Replies)
Discussion started by: wimaxpole
0 Replies

3. UNIX for Dummies Questions & Answers

Scrolling text continued

Perderabo * Unix Daemon * Join Date: Aug 2001 Location: Ashburn, Virginia Posts: 9,071 Using head and tail like that is terribly inefficient. I decided to try a rewrite. Sheesh...I spent all morning on this.... Code: #! /usr/bin/ksh # # scroller --- display text, but sleep every... (0 Replies)
Discussion started by: jamesapp
0 Replies

4. Shell Programming and Scripting

selection of context in files

I have a file like this QUEUE: <ITEM(69)> "/NLA///ACHO_EQU_IDX" Q_KEY: <ITEM(69)> "/NLA///ACHO_EQU_IDX" Q_TYPE: <VSTR(32)> "GEN_VSTR_INDEX" ... (1 Reply)
Discussion started by: manas_ranjan
1 Replies

5. UNIX and Linux Applications

command scrolling using arrow keys

hi, can anyone tell me how to enable arrow keys to scroll thru the commands on command prompt. I am using C shell ( I know, in k shell, set -o vi would enable vi command history, but set -o doesnte exist in c shell) (4 Replies)
Discussion started by: hemangi13
4 Replies

6. Shell Programming and Scripting

scrolling cursor

Hi, I'm writing scripts in perl and shell and want to add the oprion of scrolling cursor on the screen when there is no output to the screen for long time. I saw it in some script but I don't have the source code. Are anyone know how can I perform this ? Thanks (1 Reply)
Discussion started by: Alalush
1 Replies

7. AIX

vi scrolling issue

Hello, I am using poderosa to open a terminal in an aix box. The problem is when i use a sudo, the vi scroll doesnt work properly, It will scroll through the first page, but when it reaches the end, only the last line scrolls, not the whole screen, making it hard to read and work. Its like... (1 Reply)
Discussion started by: jinxor
1 Replies

8. UNIX for Dummies Questions & Answers

Help! scrolling numbers and letters

Hello all I am a unix newbie.... I have a sun netra t1 and it is freaking out I am connected to it through a console port, and it is just spitting out a ton on numbers and letters like below its just keeps going and going. I have tried rebooting it and I cannot get it back to any kind of a... (1 Reply)
Discussion started by: intraining11
1 Replies

9. UNIX for Advanced & Expert Users

selection of files based on its types

Daily we are getting some datafiles to our unix server location FTPIN. Incoming File names will be present in the location "/xyz/test/" as below: "infile_A1_YYYYMMDD", "infile_A2_YYYYMMDD", "infile_B1_YYYYMMDD", "infile_C1_YYYYMMDD" "infile_C2_YYYYMMDD" Where A, B and C are the... (3 Replies)
Discussion started by: ganapati
3 Replies

10. Shell Programming and Scripting

Vi horizontal scrolling

I have to look through logfiles where lines are several hundred characters long and if I open the log in Vi it automatically word wraps the line. In Vim you can use the -nowrap option to stop this, but how can you do this in Vi? I ask because I don't want to see the whole line, just the first few... (8 Replies)
Discussion started by: rupweb
8 Replies

Featured Tech Videos