You can use the read command to prompt for Yes/No/Cancel input in a Linux shell script.
For example:
echo “Do you want to continue? (Y/N/C)”
read answer
case “$answer” in
Y|y)
echo “Continuing…”
;;
N|n)
echo “Exiting…”
exit
;;
C|c)
echo “Cancelling…”
;;
*)
echo “Invalid input”
;;
esac
As a Linux user or administrator, there may come a time when you need to prompt a user for a simple yes, no, or cancel input in a shell script. This can be useful for a variety of purposes, such as confirming an action before executing it or providing options to a user in a user-friendly manner. In this tutorial, you will learn how to prompt for yes/no/cancel input in a Linux shell script.
How to prompt for Yes/No/Cancel input in a Linux shell script
By following the below given steps, you can prompt yes/no/cancel input in linux shell script:
- Step 1: Define the function
- Step 2: Call the function
- Step 3: Test the script
Step 1: Define the function
The first step is to define a function that will handle the user input. In this case, you will call the function “get_input”. Here’s an example of how to define the function:
get_input () { while true; do read -p "Do you want to proceed? [y/n/c]" yn case $yn in [Yy]* ) return 0;; [Nn]* ) return 1;; [Cc]* ) return 2;; * ) echo "Please answer y or n or c.";; esac done }
This function prompts the user for input by displaying the message “Do you want to proceed? [y/n/c]”. The user’s input is stored in the variable “yn”. The function then uses a case statement to determine the user’s input. If the user enters “y” or “Y”, the function returns 0. If the user enters “n” or “N”, the function returns 1. If the user enters “c” or “C”, the function returns 2. If the user enters any other input, the function displays the message “Please answer y or n or c.” and prompts the user again.
Step 2: Call the function
Once the function is defined, you can call it in your shell script. Here’s an example of how to call the function:
#!/bin/bash echo "This is a sample script that prompts for user input." if get_input; then echo "You selected yes." else echo "You selected no." fi
In this example, the script first displays the message “This is a sample script that prompts for user input.” It then calls the “get_input” function. If the user selects “yes” by entering “y” or “Y”, the script displays the message “You selected yes.” If the user selects “no” by entering “n” or “N”, the script displays the message “You selected no.” If the user selects “cancel” by entering “c” or “C”, the script does not display any message.
Step 3: Test the script
To test the script, save it to a file (e.g., “test.sh”) and make it executable by running the command “chmod +x test.sh”. Then, run the script by typing “./test.sh” in the terminal. The script should display the message “This is a sample script that prompts for user input.” and prompt the user to select “yes”, “no”, or “cancel”. The script should then display the appropriate message based on the user’s selection.
Conclusion
Prompting for yes/no/cancel input in a Linux shell script is a useful feature that can help users make informed decisions and avoid accidental actions. By defining a function that handles the user input and calling it in your script, you can easily add this feature to your own shell scripts.