How to Create Interactive Yes/No/Cancel Prompts in Linux Shell Scripts

In Linux shell scripting, it’s common to need user input for making decisions within scripts. One typical scenario is prompting the user with options like Yes, No, or Cancel. This guide will walk you through the process of creating such prompts in your shell scripts.

Understanding the Problem

When developing scripts, especially interactive ones, it’s essential to handle user input effectively. For prompts that require a simple choice between Yes, No, or Cancel, we need to ensure the script captures and processes the user’s response correctly.

1. Displaying the Prompt:
The first step is to display a prompt to the user. This prompt should clearly indicate the choices available and ask for input. Here’s how you can do it:

#!/bin/bash

echo "Do you want to proceed? (Yes/No/Cancel)"
read choice

2. Processing User Input

Next, you need to process the user’s input to determine the action to take based on their choice. In shell scripting, this often involves using conditional statements (if statements) to branch the script flow:

#!/bin/bash

echo "Do you want to proceed? (Yes/No/Cancel)"
read choice

if [ "$choice" = "Yes" ] || [ "$choice" = "yes" ]; then
    echo "Proceeding with the operation..."
    # Add your logic here for 'Yes' choice
elif [ "$choice" = "No" ] || [ "$choice" = "no" ]; then
    echo "Operation canceled."
    # Add your logic here for 'No' choice
elif [ "$choice" = "Cancel" ] || [ "$choice" = "cancel" ]; then
    echo "Operation canceled."
    # Add your logic here for 'Cancel' choice
else
    echo "Invalid choice. Please choose Yes, No, or Cancel."
    # Handle any other input scenarios
fi

3. Example Use Case:

Let’s apply this in a practical scenario where you might prompt the user before deleting a file:

#!/bin/bash

echo "Are you sure you want to delete file.txt? (Yes/No/Cancel)"
read choice

if [ "$choice" = "Yes" ] || [ "$choice" = "yes" ]; then
    rm file.txt
    echo "File deleted."
elif [ "$choice" = "No" ] || [ "$choice" = "no" ]; then
    echo "Deletion canceled."
elif [ "$choice" = "Cancel" ] || [ "$choice" = "cancel" ]; then
    echo "Operation canceled."
else
    echo "Invalid choice. Please choose Yes, No, or Cancel."
fi

Conclusion

Creating prompts for Yes/No/Cancel input in a Linux shell script involves displaying a clear message, capturing user input, and using conditional statements to handle each possible response. This approach ensures your script is interactive and user-friendly, providing clear options for decision-making.

By following these steps, you can integrate effective user prompts into your shell scripts, enhancing their usability and functionality. This method not only improves the user experience but also ensures your scripts perform actions based on user preferences, making them versatile tools in various automation and administrative tasks.

Now, armed with this knowledge, you can confidently implement user prompts in your Linux shell scripts to interact with users and guide script execution based on their input.