How to write Bash Script to display a menu, accept user data and display data?

I want to write Bash Script with user input for my destination, called the "bookstore service." Firstly, it has a menu of options, for example, “add a new book name”, “author name”, “delete book”, “update” “search”, “book list”, “Exit”. If I choose option 1, I must enter a new name for the book. How can I write user input and output data when I select the "Book List" option. I have problems with this task. Pls someone help me figure this out, it will be really helpful and thankful to all of you.

+3
source share
4 answers

Take a look at the instructions select. This allows you to create menus.

PS3="Please choose an option "
select option in go stay wait quit
do
    case $option in
        go) 
            echo "Going";;
        stay|wait) 
            echo "Standing by";;
        quit)
            break;;
     esac
done

What it looks like:

1) go
2) stay
3) wait
4) quit
Please choose an option

Edit:

One of your options may request user input:

read -rp "Enter a phrase: " phrase
echo "The phrase you entered was $phrase"
+9
source

You might want to check out Whiptail or Dialog , with which you can make graphical interfaces inside the terminal.

Here and here are good resources on how to use whiptail.

And the answers to this question are a good example of how to use Dialog.

enter image description here

+3
source

, , : http://tldp.org/LDP/abs/html/internal.html#READR

CLI:

test.sh

echo $1

CLI:

$ ./test.sh testinput
testinput
+1
source

you should check this page on how to use case / esac or choose to create a menu

Also, be sure to start reading the entire document to learn about shell scripts.

0
source

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


All Articles