read -p 'enter your choice [ 1-5 ]' choice
        case $choice in






I want the case statement to read these numbers from a file and then after entering my choice it should go to that choice.

I hope you understand my query.

  • 1
    You should check the proper syntax for case. What exactly is your question, though? And what do you mean when you say you want to "read these numbers from a file"? What's in the file? – DonHolgo Jun 18 at 14:56
  • the file has 1)abcrequest 2)defresponse 3)afgRequestReply 4)FSfReply 5)ghjrequest – Shivam Malhotra Jun 19 at 7:46

Using a select loop:

#!/usr/bin/env bash


PS3='enter your choice: '

while read -r index element; do
done < <(awk -F\) '{print $1,$2}' "$list")

select i in "${_list[@]}"; do
    case $i in
        *)  echo "Choice is ${_list[REPLY]}";;

list should be set to the path of the file containing your menu. In my case it contains:

$ cat list.txt

PS3 will be the prompt used by select

We then look through each line in list and separate the number from its value and build an array with that. _list will contain:

_list=([1]="abcrequest" [2]="defresponse" [3]="afgRequestReply" [4]="FSfReply" [5]="ghjrequest")

Now select will build a menu with each item in _list. REPLY will contain the number of your selection so using that we can do something with the value of that index in _list. (You could also just use $i directly though)

Note: this list will continuously prompt unless your code contains an exit or break statement, or if you press ctrl+c.

$ ./script.sh
1) abcrequest
2) defresponse
3) afgRequestReply
4) FSfReply
5) ghjrequest
enter your choice: 4
Choice is FSfReply
enter your choice: 5
Choice is ghjrequest
enter your choice: 2
Choice is defresponse
enter your choice: ^C
  • thanky you so much , this is exactly what I was looking for. – Shivam Malhotra Jun 24 at 14:05
  • Hi Jesse_b, can you tell why have you used done < <(awk -F) , why is there a space between those redirections , why not << . – Shivam Malhotra Jul 12 at 10:36
  • @ShivamMalhotra: It's called process substitution – Jesse_b Jul 12 at 13:16

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.