A Bash prompt with Yes, No, and Cancel options is a crucial feature in scripting for user interaction. It allows scripts to ask users for confirmation before proceeding with actions, ensuring that users have control over the process. This is typically implemented using the read
command to capture user input and case
statements to handle different responses. This setup enhances the script’s interactivity and prevents unintended operations.
To create a Bash prompt with Yes, No, and Cancel options, you can use the read
command to capture user input and case
statements to handle the different responses. Here’s a detailed breakdown:
Using read
Command:
read
command is used to take input from the user.read [options] variable_name
-p
: Prompt string.-r
: Prevent backslashes from being interpreted as escape characters.Using case
Statements:
case
statement is used to execute different blocks of code based on the value of a variable.case "$variable" in
pattern1) commands ;;
pattern2) commands ;;
*) default commands ;;
esac
#!/bin/bash
# Prompting for user input
read -p "Do you want to proceed? (y)Yes / (n)No / (c)Cancel: " choice
# Handling user input
case "$choice" in
[yY]* ) echo "You selected Yes." ;;
[nN]* ) echo "You selected No." ;;
[cC]* ) echo "You selected Cancel." ;;
* ) echo "Invalid option." ;;
esac
Prompting for Input:
read -p "Do you want to proceed? (y)Yes / (n)No / (c)Cancel: " choice
-p
option displays the prompt message.choice
is the variable where the user’s input is stored.Handling Input with case
:
case "$choice" in
[yY]* ) echo "You selected Yes." ;;
[nN]* ) echo "You selected No." ;;
[cC]* ) echo "You selected Cancel." ;;
* ) echo "Invalid option." ;;
esac
[yY]*
, [nN]*
, and [cC]*
match any input starting with ‘y’, ‘n’, or ‘c’ (case-insensitive).*
is the default case for any other input.Here’s a step-by-step guide to create a basic Bash prompt with Yes, No, and Cancel options using the read
command:
Create a new Bash script file:
nano prompt_script.sh
Add the shebang line:
#!/bin/bash
Prompt the user for input:
read -p "Do you want to proceed? (y/n/c): " choice
Handle the user’s input using a case
statement:
case "$choice" in
[yY]* ) echo "You chose Yes";;
[nN]* ) echo "You chose No";;
[cC]* ) echo "You chose Cancel";;
* ) echo "Invalid option";;
esac
Save and close the file.
Make the script executable:
chmod +x prompt_script.sh
Run the script:
./prompt_script.sh
Here’s how you can handle user input with if-else
and case
statements in a Bash script:
if-else
:#!/bin/bash
read -p "Enter your choice (yes/no/cancel): " choice
if [[ "$choice" == "yes" ]]; then
echo "You chose Yes."
elif [[ "$choice" == "no" ]]; then
echo "You chose No."
elif [[ "$choice" == "cancel" ]]; then
echo "You chose Cancel."
else
echo "Invalid choice."
fi
case
:#!/bin/bash
read -p "Enter your choice (yes/no/cancel): " choice
case "$choice" in
yes)
echo "You chose Yes."
;;
no)
echo "You chose No."
;;
cancel)
echo "You chose Cancel."
;;
*)
echo "Invalid choice."
;;
esac
Both methods prompt the user for input and handle the responses accordingly. The if-else
method uses conditional checks, while the case
method matches patterns.
Here are some practical examples of Bash scripts that utilize a prompt with Yes, No, and Cancel options:
#!/bin/bash
read -p "Do you want to install the software? (y)Yes/(n)No/(c)Cancel: " choice
case $choice in
[yY]* ) echo "Installing software...";;
[nN]* ) echo "Installation aborted.";;
[cC]* ) echo "Installation cancelled.";;
* ) echo "Invalid option.";;
esac
#!/bin/bash
read -p "Do you want to delete the file? (y)Yes/(n)No/(c)Cancel: " choice
case $choice in
[yY]* ) echo "Deleting file...";;
[nN]* ) echo "File deletion aborted.";;
[cC]* ) echo "File deletion cancelled.";;
* ) echo "Invalid option.";;
esac
#!/bin/bash
while true; do
read -p "Do you want to proceed? (yes/no/cancel): " yn
case $yn in
[Yy]* ) echo "Proceeding..."; break;;
[Nn]* ) echo "Exiting..."; exit;;
[Cc]* ) echo "Cancelled."; exit;;
* ) echo "Please answer yes, no, or cancel.";;
esac
done
These scripts demonstrate how to prompt users for input and handle their responses accordingly. Feel free to adapt them to your specific needs!
Here are some advanced techniques for enhancing a Bash prompt with Yes, No, and Cancel options, including error handling and input validation:
#!/bin/bash
read -p "Do you want to proceed? (yes/no/cancel) " response
case $response in
yes) echo "Proceeding...";;
no) echo "Exiting..."; exit 1;;
cancel) echo "Operation cancelled."; exit 0;;
*) echo "Invalid response"; exit 1;;
esac
#!/bin/bash
while true; do
read -p "Do you want to proceed? (yes/no/cancel) " response
case $response in
yes) echo "Proceeding..."; break;;
no) echo "Exiting..."; exit 1;;
cancel) echo "Operation cancelled."; exit 0;;
*) echo "Invalid response. Please enter yes, no, or cancel.";;
esac
done
#!/bin/bash
while true; do
read -p "Do you want to proceed? (yes/no/cancel) " response
case $response in
yes) echo "Proceeding..."; break;;
no) echo "Exiting..."; exit 1;;
cancel) echo "Operation cancelled."; exit 0;;
*) echo "Error: Invalid input. Please type 'yes', 'no', or 'cancel'.";;
esac
done
#!/bin/bash
while true; do
read -p "Do you want to proceed? (yes/no/cancel) " response
response=$(echo "$response" | tr '[:upper:]' '[:lower:]')
case $response in
yes) echo "Proceeding..."; break;;
no) echo "Exiting..."; exit 1;;
cancel) echo "Operation cancelled."; exit 0;;
*) echo "Invalid response. Please enter yes, no, or cancel.";;
esac
done
#!/bin/bash
read -t 10 -p "Do you want to proceed? (yes/no/cancel) " response
if [ $? -ne 0 ]; then
echo "Timed out waiting for user input."
exit 1
fi
case $response in
yes) echo "Proceeding...";;
no) echo "Exiting..."; exit 1;;
cancel) echo "Operation cancelled."; exit 0;;
*) echo "Invalid response"; exit 1;;
esac
These techniques ensure your script handles user input robustly, providing clear feedback and handling errors gracefully.
Creating a bash prompt with yes, no, and cancel options is a useful technique for scripting tasks that require user input. This approach allows users to confirm or cancel actions, making the script more interactive and user-friendly.
To create such a prompt, you can use a while loop to continuously ask for user input until they enter one of the allowed responses.
while
loop to repeatedly ask for user input.read
command with the -p
option to display a prompt message.case
statement to evaluate the user’s response and perform different actions based on their choice.tr
command to convert the user’s response to lowercase, making the script case-insensitive.read
command with the -t
option followed by the desired timeout value in seconds.This approach is particularly useful when scripting tasks that require user confirmation or cancellation, such as deleting files, modifying system settings, or executing critical operations. By incorporating a yes, no, and cancel prompt, you can ensure that your script handles user input robustly and provides clear feedback to users.