English 中文(简体)
Bash Case menu - dynamic choices
原标题:

I m not sure what the policy is here on asking followup questions. So please excuse me if i m breaking protocol. Earlier I was constructing a menu in bash ( Here )

And so far I ve got it working really good. Code 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

Except - that works great for menu items up to 9. The menu will be dynamic - could have 1 item, 20 items, or 197 items. I ve tried changing [0-9] to be [0-9][0-9] to see if it would take 12. But it doesn t. Can anyone put me on the right path? I suppose I could just remove the [0-9] part and take anything that is not q. But wouldn t it be better to look for numbers?

Thank you in advance.

最佳回答

I would do some validation on $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.
问题回答

Here s what I got to work. The primary differences are the addition of shopt -s extglob, which turns on extended pattern matching, and the +([0-9]) pattern, which is the bash equivalent of regular expression [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




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签