Close Menu
    Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»Bash Script – Prompt to Confirm (YES/NO/CALCEL)

    Bash Script – Prompt to Confirm (YES/NO/CALCEL)

    By RahulMarch 22, 20233 Mins Read

    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.

    Outline:

    1. Understanding Bash scripting basics
    2. Writing the script
    3. Testing and debugging the script
    4. Real-world use case

    Understanding Bash scripting 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.

    Writing the 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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    #!/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.

    bash script
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    An Introduction to Bash Variables

    An Introduction to Bash Variables

    Bash LOCAL and GLOBAL Variables

    Bash LOCAL and GLOBAL Variables

    Bash Script to Reverse a Number

    View 4 Comments

    4 Comments

    1. ajay on June 7, 2019 8:02 am

      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

      Reply
    2. Raj on December 16, 2018 3:07 am

      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

      Reply
    3. Jeff Fritz on March 28, 2017 6:29 pm

      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

      Reply
      • Rahul K. on March 29, 2017 2:23 am

        Thanks Jeff,

        The article has been updated.

        Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Change Port in Next.Js
    • Ubuntu 24.04 LTS: The Future of Open-Source Excellence
    • How to Execute Linux Commands in Python
    • Creating MySQL User with GRANT OPTION
    • Where to find crontab (cron) logs in Ubuntu & Debian
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.