Interactive Array Menu


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Interactive Array Menu
# 1  
Interactive Array Menu

This should be simple, but i haven't done it before...

KSH

I am reading a file into an array and currently displaying the values to the screen. What I need to do is to display a subset of those values, likely numbered, and prompt the user to select one. When they enter the number, it retrieves the assoicated array value and outputs it to the screen. (this will be used for more complex functions later)

For example, the program is run and the first 10 rows are displayed. I select a number or hit a character to proceed to the next page. This would dump the first 10 rows and load the next 10 (into another array?) I select a value and the row associated with that value echoes to the screen.

Any suggestions are greatly appreciated.
My code so far:
Code:
#!/bin/ksh
 
# define array
set -A line_array
# select input file
file_name="$scripts/cb_list.dat"
 
# populate the array
i=0
while read file_line
do
line_array[i]=${file_line}
let i=${i}+1
done < ${file_name}
 
i=0
while [ ${i} -le ${#line_array[*]} ]
do 
echo ${line_array[i]}
let i=$i+1
done
# end of variable_define.sh



Last edited by gecko2424; 07-25-2008 at 10:48 AM..
# 2  
No need to 'dump' the values since you already have all of them loaded in the array, you can just calculate the current page offset, (say 10 for the second page), and just display items 11-20 in the array for that page.

Your code looks fine so far, what part are you having difficulty with?
# 3  
EDIT:
I have it working now, code is below for anyone that it might help. I did what i needed with a nested function.

Code:
#!/bin/ksh
# define array
set -A line_array

# select input file
file_name="cb_list.dat"


# populate the array
i=1

while read file_line
do
   line_array[i]=${file_line}
   let i=${i}+1
done < ${file_name}


#Display selection options
i=1
x=10

var_select ()
{
while [ ${i} -le ${x} ] && [ ${i} -lt ${#line_array[*]} ]
do
 echo
 echo $i ${line_array[i]}
  let i=$i+1
done

#Select an option

    echo
    echo "Select an option number or press enter: \c"
    read num
disp_select
}


disp_select ()
{
if [ ${num} -le ${i} ]
  then
    echo "You have selected: ${line_array[num]}"
else
    x=${x}+10
    var_select
fi
}

var_select


# end


Last edited by gecko2424; 07-31-2008 at 02:41 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #979
Difficulty: Medium
macOS is based on the Unix operating system and on technologies developed between 1985 and 1997 at NeXT.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Gnome 3.28.3 menu item dissapears under the system menu

I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome. Unfortunately, I never found a... (1 Reply)
Discussion started by: bodisha
1 Replies

2. Shell Programming and Scripting

Passing positioning array as whiptail -- menu option

I may have asked this before, so forgive OF. Problem: I can pass positioning array as -- menu option to whiptail, but it does not show in the whiptail form as an array - only single (first member "lsusb" ) entry / line shows up. Code: DynamicEntry=$(whiptail \ --title "DEBUG... (1 Reply)
Discussion started by: annacreek
1 Replies

3. Homework & Coursework Questions

Dialog menu with array

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi I'm currently making a schoolproject and I'm stuck with reading an array into the possible options of a... (1 Reply)
Discussion started by: dan007255
1 Replies

4. Shell Programming and Scripting

Dialog menu with array

Hi I'm currently making a schoolproject and I'm stuck with reading an array into the possible options of a menu dialog. What I mean is: I've put some values into an array, I want all these values from the array to be possible to be selected in the menu. Please help :) I'm programming in Debian... (1 Reply)
Discussion started by: dan007255
1 Replies

5. Red Hat

Interactive PXE Boot Menu

I have been asked to modify our PXE server such that there will be only one entry in the pxelinux.cfg/default file, where the same kernel and initrd.img will be used regardless of what operating system is to be installed, and the user will type in the path to the kickstart file that will be used. ... (7 Replies)
Discussion started by: ceb
7 Replies

6. Shell Programming and Scripting

Execute interactive bash menu script in browser with PHP

I have an interactive menu script written in bash and I would like use PHP to open the interactive bash menu in a browser. Is this possible? Using the sytem() function in php runs the script but it's all garbled. Seems like maybe a terminal window needs to be opened in php first? ... (1 Reply)
Discussion started by: nck
1 Replies

7. Homework & Coursework Questions

How to write script that behaves both in interactive and non interactive mode

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (8 Replies)
Discussion started by: rits
8 Replies

8. Homework & Coursework Questions

Help with Interactive / Non Interactive Shell script

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (1 Reply)
Discussion started by: rits
1 Replies

9. Shell Programming and Scripting

Menu help with array selection

Hi there all I got the following I got multiple arrays named for example STAT_AAAA STAT_AAAB STAT_AAAC STAT_AAAD Now what I want I have chosen an option in a menu to select 1 but I dont want to write for all the same thing so I made it a signle one now what I want is to get STAT_ and... (6 Replies)
Discussion started by: draco
6 Replies

10. Shell Programming and Scripting

Interactive menu

Hi ,some time ago i did an interactive menu based on eval function for navigation , one of the post remind me it. I think it could be handy for others. #!/usr/bin/ksh keyRead () { tput smso echo "Enter option." tput rmso oldstty=$(stty -g) stty -icanon -echo min 1 time 1 Answer=$(dd... (0 Replies)
Discussion started by: Klashxx
0 Replies

Featured Tech Videos