We have a simple menu with prompt of menu numbers to user.
It is still under construction.
Is there a way to "count" the menu choices so the prompt maximum count can be changed dynamically?
See attached TODO note in code
Code:
read_options(){
local choice
# the max choice is static
#TODO add max choice as variable
read -p "Enter choice [ 1 - 3] " choice
case $choice in
1) one ;;
2) two ;;
3) three ;;
4) select_command_DEBUG "pass command to function" "lsusb" ;; #pass$
5) select_command_DEBUG " menu 5 pass list_wlan_interfaces() to func$
" list_wlan_interfaces " ;;
6) pause ;; # back to GUI exit 0;;
*) echo -e "${RED}Error...${STD}" && sleep 2
esac
}
#!/usr/bin/bash
files='*sh';
actions='cat edit quit'
PS3="Pick one of the above [${actions}]: "
TMOUT=10
select i in ${actions}
do
case $i in
cat) eval cat "${files}" ;;
edit) eval ${EDITOR-vi} "${files}" ;;
quit) break ;;
"") print -u2 you must select one of the above [${actions}] ;;
esac
done
Sure, but it just replaces the "menu" static text structure with static array.
Since "case" gets executed in one pass I would have to make two passes. Make first one to search for invalid option and somehow count the number of steps taken until the "not found option" error is executed.
Sure, but it just replaces the "menu" static text structure with static array.
Since "case" gets executed in one pass I would have to make two passes. Make first one to search for invalid option and somehow count the number of steps taken until the "not found option" error is executed.
That would be my "max option" value.
Naw, this is too convoluted and silly,
Hm.. I don't quite understand the comments and the counting part in particular.
You must have the list of available menu choices stored somewhere: hardwired, read from file etc... I've just provided a sample using select with the hardwired menu choices in the form of an array - this is just a sample showing the select capability WITH the dynamic prompt choices.
What are you trying to do with counting?
I'm confused as what you're after. You'll have to explain it again...
Sure, but it just replaces the "menu" static text structure with static array.
Since "case" gets executed in one pass I would have to make two passes. Make first one to search for invalid option and somehow count the number of steps taken until the "not found option" error is executed.
That would be my "max option" value.
Naw, this is too convoluted and silly,
select uses two variables: the one you pass it; and REPLY. Consider this :
Code:
$ PS3='enter choice '
$ select choice in bish bash bosh
> do
> printf "choice is %s; REPLY is %s\n" "$choice" "$REPLY"
> done
1) bish
2) bash
3) bosh
enter choice 1
choice is bish; REPLY is 1
enter choice 2
choice is bash; REPLY is 2
enter choice 3
choice is bosh; REPLY is 3
enter choice 4
choice is ; REPLY is 4
enter choice fred
choice is ; REPLY is fred
enter choice
choice contains the keyword that you want as a menu item; REPLY contains what was actually entered.
Bash has hash arrays, so you could do something like:
Code:
declare -A mymenu
...
mymenu["some_key"]="some value"
...
select mychoice in "${!mymenu[@]}"
do
if [[ -z "$mychoice" ]]
then
echo "bad choice %s - try again\n" "$REPLY"
continue
fi
# use $mychoice or ${mymenu[$mychoice]} here
done
EDIT
Okay, after re-reading your original post I realise I was misinterpretting what you meant by a dynamic menu.
Code:
read_options(){
local choice
local -a choices one two three debug pause
local PS3
printf -vPS3 "Enter choice [1 - %d] " ${#choices}
# the max choice is static
#TODO add max choice as variable
# read -p "Enter choice [ 1 - 3] " choice
select choice "${choices[@]}"
do
#case $choice in
case $REPLY in
1) one ;;
2) two ;;
3) three ;;
4) select_command_DEBUG "pass command to function" "lsusb" ;; #pass$
5) select_command_DEBUG " menu 5 pass list_wlan_interfaces() to func$
" list_wlan_interfaces " ;;
6) pause ;; # back to GUI exit 0;;
*) echo -e "${RED}Error...${STD}" && sleep 2
esac
done
}
Your "dynamic" menu isn't very flexible as you have problems when you want to add something in the middle.
Consider
Code:
select choice in fred ginger mary
do case choice in
fred) do_fred ;;
ginger) do_ginger ;;
mary) do_mary ;;
esac
done
Code:
select choice in fred mike ginger mary
do case choice in
fred) do_fred ;;
ginger) do_ginger ;;
mary) do_mary ;;
mike) do_mike ;;
esac
done
I didn't have to put the case item "mike" in the same position in the case statement; I can also move the other options around and leave the case statement as-is. No, you don't have the number of items readily available (unless you use the array above, which could make the select statement more readable) but the choice variable will be empty if the user enters the wrong thing.
In my opinion that makes select more flexible than your 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)
Hey,
I am trying to compare formated login and logout dates from one user at a host which I have stored in a tmp directory in order to find out the total login time. I need to compare them in order to find overlapping intervals.
At first I tried to store each log in and logo date in an array... (3 Replies)
The perl command is not executing? I am trying to run the .pl in my cygwin home directory (C:\cygwin\home\cmccabe) using ${id}.txt.hg19_multianno.txt (located in the annovar directory) as the input file to be formatted and $FILENAME is the output file to be saved. The .pl is attached as... (8 Replies)
Hi all,
I'm using Bash 4.3.8 on an Ubuntu system, and no matter what I try, incrementing a counter won't work. The simplest example would be something like this:
#!/bin/bash
myVar=0
myVar=$((myVar++))
echo myVar
The variable should be 1, but it's always 0. I've tried every increment... (6 Replies)
Hello,
I'm new here. I test these expressions's value in my script :
(in centOS 6 )
#!/bin/bash
array='something'
echo "############"
echo ${array}
echo ${array}
echo ${array}
echo "############"
The output result is :
#################
something
something
#################... (5 Replies)
Hi again :)
This is just a sample whiptail menu.
Works great, but have been trying to get the chosen value into a variable but failing pretty bad...its ther but unsure how to echo it out when needed
#! /bin/bash
#This is the menu
whiptail --title "Menu example" --menu "Choose an... (9 Replies)
Hello Everyone,
I'm in need of assistance on creating a script with a counter on a certain string.
Basically this script opens a log file and displays certain log data. There are two key words in the log. START and FINISH. In between the START and FINISH is a variable ACTNUMBER. It will... (1 Reply)
I have looked high and low, tryed lots of diffrent things but cant get a simple counter to work right.
what i need is to increase a count ever time it finishes the test, pass or fail. example TEST PASS 1, NEXT TEST PASS 2,
I curently have
set foo o
while {$foo <=5} {
incr foo
puts... (1 Reply)
I have a main menu
quit=n
while
do
clear
echo
echo "1. General system information"
echo "2. Hardware utilisation information"
echo "3. File management"
echo "4. User information"
echo "5. Information on network connectivity"
echo "6. Information on processes"
echo "Q.Quit" ... (3 Replies)