Bash scripts are incredibly useful for automating tasks and simplifying complex processes. In this article, we will delve into the creation of an interactive Bash script that prompts the user for confirmation using a YES/NO/CANCEL dialogue. This feature can be extremely helpful when you want to ensure that users are aware of the actions they’re about to execute and give them the option to cancel if needed.

Advertisement

Understanding Bash Script Basics

Before we begin, it’s crucial to understand the fundamentals of Bash scripting. Bash is a Unix shell, which is a command-line interface for interacting with an operating system. A Bash script is a sequence of commands that are executed in a specific order. The script can include variables, loops, and conditional statements, allowing you to create complex and powerful scripts.

Write a Bash Script

Let’s start by creating a new file called confirm.sh and opening it in your preferred text editor. In this example, we’ll be using the nano editor:

nano confirm.sh 

Now, let’s write the script. Here’s an example of a simple interactive Bash script that prompts the user for a YES/NO/CANCEL response:


#!/bin/bash

# Function to display the confirmation prompt
function confirm() {
    while true; do
        read -p "Do you want to proceed? (YES/NO/CANCEL) " yn
        case $yn in
            [Yy]* ) return 0;;
            [Nn]* ) return 1;;
            [Cc]* ) exit;;
            * ) echo "Please answer YES, NO, or CANCEL.";;
        esac
    done
}

# Example usage of the confirm function
if confirm; then
    echo "User chose YES. Executing the operation..."
    # Place your code here to execute when user confirms
else
    echo "User chose NO. Aborting the operation..."
    # Place your code here to execute when user denies
fi

Save and close the file. To make the script executable, run the following command:

chmod +x confirm.sh 

Testing and debugging the script

Now that we have our script, it’s essential to test it to ensure it works as intended. Run the script using the following command:

./confirm.sh 

You should see the following prompt:

Output:
Do you want to proceed? (YES/NO/CANCEL)

Test the script by entering YES, NO, and CANCEL to see if the appropriate responses are executed.

Real-world use case

This interactive confirmation prompt can be easily integrated into other Bash scripts that require user confirmation before performing critical tasks. For example, you could use it in a script that deploys changes to a production environment, deletes files, or updates system configurations. This simple yet powerful feature adds an extra layer of security and user control to your scripts.

Conclusion

In this article, we’ve shown you how to create an interactive Bash script that prompts users for confirmation with a YES/NO/CANCEL dialogue. This can be a valuable addition to your scripts, ensuring users are aware of the actions they’re about to execute and giving them the option to cancel if needed. With this knowledge, you can create more robust and user-friendly Bash scripts for various tasks and applications.

Share.

4 Comments

  1. In the 2nd example. loops goes on infinity even if user type yes or no.
    use break statement to terminate the loop upon valid input.

    while true
    do
    read -r -p “Are You Sure? [Y/n] ” input

    case $input in
    [yY][eE][sS]|[yY])
    echo “Yes”
    break
    ;;
    [nN][oO]|[nN])
    echo “No”
    break
    ;;
    *)
    echo “Invalid input…”
    ;;
    esac
    done

  2. It goes to infite loop. didn’t ask for user input !

    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process
    test.ksh[4]: read: no query process

  3. Thanks for your page, but there is a bug here –

    This – is incorrect. You need to drop the semicolon at the end of the line ‘while true;’
    while true:
    do

    This is correct –
    while true
    do

Exit mobile version