Sponsored Content
Top Forums Shell Programming and Scripting Array based selection of values by selecting either index or member Post 303045472 by Chubler_XL on Thursday 26th of March 2020 04:37:10 PM
Old 03-26-2020
Nice procedure nezabudka, but original request was selection of item by number or by name:

Quote:
<- user enters either number corresponding to entry or the name
For a higher level of difficulty how about spaces in the options:

Code:
#!/bin/bash
listofitems="fried rice,boiled rice,beans,carrots,radishes,rutabaga,spinach"
IFS=, MU=( $listofitems quit )

while true
do
    echo "Please enter the veg you like from"
    select opt in "${MU[@]}"
    do
        [ "$opt" != "" ] && break 3
        # Check REPLY against the array:
        for opt in ${MU[@]}
        do
           [ "$REPLY" = "$opt" ] && break 3
        done
        echo "Illegal selection - try again"
        break
    done
done
echo "You typed $REPLY and selected $opt"


Code:
$ ./menu
Please enter the veg you like from
1) fried rice   3) beans        5) radishes     7) spinach
2) boiled rice  4) carrots      6) rutabaga     8) quit
#? none
Illegal selection - try again
Please enter the veg you like from
1) fried rice   3) beans        5) radishes     7) spinach
2) boiled rice  4) carrots      6) rutabaga     8) quit
#? boiled rice
You typed boiled rice and selected boiled rice

$ ./menu
Please enter the veg you like from
1) fried rice   3) beans        5) radishes     7) spinach
2) boiled rice  4) carrots      6) rutabaga     8) quit
#? 7
You typed 7 and selected spinach

If you have bash 4.0 or higher use below for case insensitive matching (eg type QUIT instead of quit)
[ "${REPLY,,}" = "${opt,,}" ] && break 3

Last edited by Chubler_XL; 03-26-2020 at 06:12 PM..
This User Gave Thanks to Chubler_XL For This Post:
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies

2. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

3. Programming

random array index returning values not contained

For kicks I wrote up a Password generator after lunch. Let me start with the code: unsigned int x,y,z,c; unsigned int KISS(); unsigned int devrand(); int main( int argc, char** argv ) { int i, j = 1; char pwd = "abcdefghijklmnopqrstuvwxyz" ... (5 Replies)
Discussion started by: VRoemer
5 Replies

4. Shell Programming and Scripting

PERL - Selecting specific files based on 'date stamp' values

Hi, I've list of files in a directory, which have date stamp value in their names. ex: abc_data_20071102.csv, abc_data_20091221.csv, abc_data_20100110.csv, abc_data_20100222.csv, abc_data_20080620.csv,... etc., I need to select and process only files, within the given date... (4 Replies)
Discussion started by: ganapati
4 Replies

5. Shell Programming and Scripting

Selecting rows based on values in columns

Hi My pipe delimited .txt file contains rows with 10 columns. Can anyone advise how I output to file only those rows with the letters ‘ci' as the first 2 characters in the 3rd column ? Many thanks (4 Replies)
Discussion started by: malts18
4 Replies

6. Programming

Selecting array values

I have two arrays DIST(1:NCOF) and X(1:NX) Let NCOF = 5 and NX = 15, with DIST = and X = I want to create an array that puts a zero if DIST is outside the region in X, otherwise putting 1. In this example I should get RES = Using DIST = would give RES = The values in... (6 Replies)
Discussion started by: kristinu
6 Replies

7. Shell Programming and Scripting

Selecting lowest and highest values in columns 1 and 2, based on subsets in column 3

Hi, I have a file with the following columns: 361459 447394 CHL1 290282 290282 CHL1 361459 447394 CHL1 361459 447394 CHL1 178352861 178363529 AGA 178352861 178363529 AGA 178363657 178363657 AGA Essentially, using CHL1 as an example. For any line that has CHL1 in... (2 Replies)
Discussion started by: hubleo
2 Replies

8. Shell Programming and Scripting

build array name based on loop index

Hi, I am new to perl and I have the following query please help here. I have following array variables declaration @pld1 = qw(00 01 02 03 04 05); @pld2 = qw(10 11 12 13 14 15); for(my $k=1;$k<=2;$k++) { //I want here to use @pld1 if $k is 1 // and @pld2 if $k is 2. How to do... (3 Replies)
Discussion started by: janavan
3 Replies

9. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

10. Shell Programming and Scripting

Find minimum and maximum values based on column with associative array

Hello, I need to find out the minimum and maximum values based on specific column, and then print out the entire row with the max value. Infile.txt: scf6 290173 290416 . + X_047241 T_00113118-1 scf6 290491 290957 . + X_047241 T_00113118-2 scf6 290898 290957 . + X_047241 T_00113119-3 scf6... (2 Replies)
Discussion started by: yifangt
2 Replies
XtOwnSelection(3)						   XT FUNCTIONS 						 XtOwnSelection(3)

NAME
XtOwnSelection, XtOwnSelectionIncremental, XtDisownSelection - set selection owner SYNTAX
Boolean XtOwnSelection(Widget w, Atom selection, Time time, XtConvertSelectionProc convert_proc, XtLoseSelectionProc lose_selection, XtSe- lectionDoneProc done_proc); Boolean XtOwnSelectionIncremental(Widget w, Atom selection, Time time, XtConvertSelectionIncrProc convert_callback, XtLoseSelectionIncrProc lose_callback, XtSelectionDoneIncrProc done_callback, XtCancelConvertSelectionProc cancel_callback, XtPointer client_data); void XtDisownSelection(Widget w, Atom selection, Time time); ARGUMENTS
convert_proc Specifies the procedure that is to be called whenever someone requests the current value of the selection. done_proc Specifies the procedure that is called after the requestor has received the selection or NULL if the owner is not interested in being called back. lose_selection Specifies the procedure that is to be called whenever the widget has lost selection ownership or NULL if the owner is not inter- ested in being called back. selection Specifies an atom that describes the type of the selection (for example, XA_PRIMARY, XA_SECONDARY, or XA_CLIPBOARD). time Specifies the timestamp that indicates when the selection ownership should commence or is to be relinquished. w Specifies the widget that wishes to become the owner or to relinquish ownership. DESCRIPTION
The XtOwnSelection function informs the Intrinsics selection mechanism that a widget believes it owns a selection. It returns True if the widget has successfully become the owner and False otherwise. The widget may fail to become the owner if some other widget has asserted ownership at a time later than this widget. Note that widgets can lose selection ownership either because someone else asserted later own- ership of the selection or because the widget voluntarily gave up ownership of the selection. Also note that the lose_selection procedure is not called if the widget fails to obtain selection ownership in the first place. The XtOwnSelectionIncremental procedure informs the Intrinsics incremental selection mechanism that the specified widget wishes to own the selection. It returns True if the specified widget successfully becomes the selection owner or False otherwise. For more information about selection, target, and time, see Section 2.6 of the Inter-Client Communication Conventions Manual. A widget that becomes the selection owner using XtOwnSelectionIncremental may use XtDisownSelection to relinquish selection ownership. The XtDisownSelection function informs the Intrinsics selection mechanism that the specified widget is to lose ownership of the selection. If the widget does not currently own the selection either because it lost the selection or because it never had the selection to begin with, XtDisownSelection does nothing. After a widget has called XtDisownSelection, its convert procedure is not called even if a request arrives later with a timestamp during the period that this widget owned the selection. However, its done procedure will be called if a conversion that started before the call to XtDisownSelection finishes after the call to XtDisownSelection. SEE ALSO
XtAppGetSelectionTimeout(3), XtGetSelectionValue(3) X Toolkit Intrinsics - C Language Interface Xlib - C Language X Interface X Version 11 libXt 1.1.4 XtOwnSelection(3)
All times are GMT -4. The time now is 02:22 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy