Home > Enterprise >  Naming menu options
Naming menu options

Time:11-12

I'm looking to name my menu options, it currently looks like this -

1) Option 1
2) Option 2
3) Option 3
4) Quit

I tried adding menu descriptions by editing the following but it returns no results when you enter a menu number

options=("Option 1 - Search with no date filter. SLOW!!" "Option 2 - Search via year" "Option 3 - Full date search" "Quit")

I would like it to look like this -

1) Option 1 - Search with no date filter. SLOW!!
2) Option 2 - Search via year
3) Option 3 - Full date search
4) Quit

Full script -

#!/bin/bash
echo ""
PS3='Please enter your choice: '
echo ""
#options=("Option 1 - Search with no date filter. SLOW!!" "Option 2 - Search via year" "Option 3 - Full date search" "Quit")
options=("Option 1" "Option 2" "Option 3" "Quit")
echo ""
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            echo "" 
            echo Please enter the telephone number?
            echo ""
            read vartel
            echo ""
            grep -e $vartel /root/hourly/cdr_export-hourly-*.csv
            echo ""
            ;;
        "Option 2")
            echo "" 
            echo Please enter the telephone number?
            echo ""
            read vartel
            echo ""
            echo Please enter the last two year digits?
            echo ""
            read varyear
            echo ""
            grep -e $vartel /root/hourly/cdr_export-hourly-$varyear*.csv
            echo ""
            ;;
        "Option 3")
            echo "" 
            echo Please enter the telephone number?
            echo ""
            read vartel
            echo ""
            echo Please enter the last two year digits?
            echo ""
            read varyear
            echo ""
            echo Please enter the month?
            echo ""
            read varmonth
            echo ""
            echo Please enter the day?
            echo ""
            read varday
            echo ""
            grep -e $vartel /root/hourly/cdr_export-hourly-$varyear-$varmonth-$varday*.csv
            echo ""
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

CodePudding user response:

How about:

case "$opt" in
  "Option 1"*)
    ...
    ;;
  "Option 2"*)
    ...
    ;;
  "Quit")
    break
    ;;
  *)
    ...
    ;;
esac

CodePudding user response:

you need modify the pattern string in each case like below

case $opt in
        "Option 1 - Search with no date filter. SLOW!!")

::: :::

"Option 2 - Search via year")

::: :::

"Option 3 - Full date search")

version of your full script below.

#!/bin/bash
echo ""
PS3='Please enter your choice: '
echo ""
options=("Option 1 - Search with no date filter. SLOW!!" "Option 2 - Search via year" "Option 3 - Full date search" "Quit")
#options=("Option 1" "Option 2" "Option 3" "Quit")
echo ""
select opt in "${options[@]}"
do
    case $opt in
        "Option 1 - Search with no date filter. SLOW!!")
            echo "" 
            echo Please enter the telephone number?
            echo ""
            read vartel
            echo ""
            grep -e $vartel /root/hourly/cdr_export-hourly-*.csv
            echo ""
            ;;
        "Option 2 - Search via year")
            echo "" 
            echo Please enter the telephone number?
            echo ""
            read vartel
            echo ""
            echo Please enter the last two year digits?
            echo ""
            read varyear
            echo ""
            grep -e $vartel /root/hourly/cdr_export-hourly-$varyear*.csv
            echo ""
            ;;
        "Option 3 - Full date search")
            echo "" 
            echo Please enter the telephone number?
            echo ""
            read vartel
            echo ""
            echo Please enter the last two year digits?
            echo ""
            read varyear
            echo ""
            echo Please enter the month?
            echo ""
            read varmonth
            echo ""
            echo Please enter the day?
            echo ""
            read varday
            echo ""
            grep -e $vartel /root/hourly/cdr_export-hourly-$varyear-$varmonth-$varday*.csv
            echo ""
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

output:

 % ./shell.sh



1) Option 1 - Search with no date filter. SLOW!!
2) Option 2 - Search via year
3) Option 3 - Full date search
4) Quit
Please enter your choice: 3

Please enter the telephone number?

1234567890

Please enter the last two year digits?

98

Please enter the month?

12

Please enter the day?

31

grep: /root/hourly/cdr_export-hourly-98-12-31*.csv: No such file or directory

Please enter your choice: 5
invalid option 5
Please enter your choice: 3

Please enter the telephone number?

^C
  • Related