3

When you use the select loop to present the elements of an array as an option, it will display it all at one time. Is there a way to present the options to the user for like 25 options at a time?

  • Show an example. Copy 25 elements of your array to a new one and use the new one? – Cyrus Nov 13 '15 at 5:34
  • If any of the existing answers solves your problem, please consider accepting it via the checkmark. Thank you! – Jeff Schaller Apr 23 '17 at 12:59
0

I'd never used select before, and I've also never used bash arrays before, so here's v0.1 of a menu that cycles through 25 items at a time. The number of items at a time is set up top as howmany. You'd need to add logic for circling back around to the beginning. It also doesn't do anything interesting with your menu selection. You may want to break out when the user picks an actual menu item.

The interesting parts are refilling the CURRNUMS array based on the starting and ending indices and including an extra "next" item in the select loop.

ALLNUMBERS=({30..90})
start=0
howmany=25
while :
do
  end=$((start + howmany - 1))
  CURRNUMS=
  for (( i = start; i < end ; i++ ))
  do
    cindex=$((i - start))
    CURRNUMS[$cindex]=${ALLNUMBERS[$i]}
  done
  echo Presenting a new set of menu items...
  select number in ${CURRNUMS[*]} next; do
    echo You picked $REPLY
    echo number is now: $number
    if [ $REPLY = "next" ] || [ $REPLY = $howmany ] ; then
      start=$end
      break;
    fi
  done
done

Here's sample output:

Presenting a new set of menu items...
1) 30      5) 34     9) 38    13) 42    17) 46    21) 50    25) next
2) 31      6) 35    10) 39    14) 43    18) 47    22) 51
3) 32      7) 36    11) 40    15) 44    19) 48    23) 52
4) 33      8) 37    12) 41    16) 45    20) 49    24) 53
#? 2
You picked 2
number is now: 31
#? 25
You picked 25
number is now: next
Presenting a new set of menu items...
1) 54      5) 58     9) 62    13) 66    17) 70    21) 74    25) next
2) 55      6) 59    10) 63    14) 67    18) 71    22) 75
3) 56      7) 60    11) 64    15) 68    19) 72    23) 76
4) 57      8) 61    12) 65    16) 69    20) 73    24) 77
#? 3
You picked 3
number is now: 56
#? next
You picked next
number is now:
Presenting a new set of menu items...
1) 78      3) 80     5) 82     7) 84     9) 86    11) 88    13) 90
2) 79      4) 81     6) 83     8) 85    10) 87    12) 89    14) next
#? ^C
0

I suggest you make a bash function that takes starting element and ending element of what you want to display in an array, that makes a recursive call to itself later. Parts of the array can displayed with bash's parameter expansion qualities ( ${VARIABLE:start:off-set} )

Sample script

#!/bin/bash

function selectStuff
{

select item in   ${ARRAY[@]:$1:$2};
do
  echo "You selected" $item
  if [ "$item" == "25"  ];then
    selectStuff 26 50
  fi
done

}

ARRAY=($(seq 1 50))
selectStuff 0 25

Demo

./selecScript.sh
1) 1     4) 4    7) 7   10) 10  13) 13  16) 16  19) 19  22) 22  25) 25
2) 2     5) 5    8) 8   11) 11  14) 14  17) 17  20) 20  23) 23
3) 3     6) 6    9) 9   12) 12  15) 15  18) 18  21) 21  24) 24
#? 25
You selected 25
1) 27    4) 30   7) 33  10) 36  13) 39  16) 42  19) 45  22) 48
2) 28    5) 31   8) 34  11) 37  14) 40  17) 43  20) 46  23) 49
3) 29    6) 32   9) 35  12) 38  15) 41  18) 44  21) 47  24) 50

#? 1
You selected 27
#? ^C
xieerqi@eagle:~$ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.