Bash Application Menu - Dynamic Selection

I am not sure which policy here asks questions about the next steps. So please excuse me if I break the protocol. I used to build a menu in bash ( here )

And so far it worked very well for me. The code is here.

while [[ 1 ]]
do
    cat -n "$dumpfile"
    read -p "Please make a selection, select q to quit: " choice
    case $choice in
            # Check for digits
    [0-9] )   dtvariable=$(sed -n "$choice"p "$dumpfile")
              $dtvariable            ;;
     q|Q)
         break
           ;;
      *)
           echo "Invalid choice"
           ;;
    esac
done

In addition, this works great for menu items up to 9. The menu will be dynamic - it can have 1 item, 20 items, or 197 items. I tried changing [0-9] to [0-9] [0-9] to see if it would be 12. But it is not. Can someone put me on the right track? I suppose I could just delete part [0-9] and take everything that is not q. But isn’t it better to look for numbers?

Thanks in advance.

+3
2

$choice:

case $choice in
     # Check for digits
    +([0-9]))
        lines=($(wc -l ))
        if (( choice > 0 && choice <= lines ))
        then
            dtvariable=$(sed -n "$choice"p "$dumpfile")
            $dtvariable            ;;
        fi
# etc.
+2

. shopt -s extglob, +([0-9]), bash [0-9]+

shopt -s extglob
while [[ 1 ]]
do
    read -p "Please make a selection, select q to quit: " choice
    case $choice in
            # Check for digits
    +([0-9]))  
         echo $choice ;;
     q|Q)
         break
           ;;
      *)
           echo "Invalid choice"
           ;;
    esac
done
+1

Source: https://habr.com/ru/post/1722537/


All Articles