• Skip to primary navigation
  • Skip to main content
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

bash scripting : Prompt user yes / no for confirmation

March 13, 2015 by Sharad Chhetri

In this tutorial we will learn simple bash script to prompt user yes / no for confirmation. It means when user run the script, it will first ask user to confirm by typing yes or no. Once user confirm it take appropriate action. Whereas in case, the user type apart of yes/no , it will print ‘invalid answer’ .

In this bash script we have added in-case sensitive choice by user , means no matter if user type yes or no in small or capital letter.

Steps to create the bash script

1. Create a bash script file , here in this example we have created file called test.sh

vi test.sh 

2. Now copy and paste the below given contents in test.sh file. After this save and exit.

#!/bin/bash

read -p "Do you want to run command 'uname -ar' ? (yes/no)" reply

choice=$(echo $reply|sed 's/(.*)/L1/')

if [ "$choice" = 'yes' ] 
then
  echo "Welcome you selected yes, wait for 3 seconds to get output";
  sleep 3;
  uname -ar;

elif [ "$choice" = 'no'  ]
then

  echo "You selected 'no', hence exiting in 3 seconds";
  sleep 3
  exit 0
else
echo "invalid answer, type yes or no";
fi

3. Make the test.sh file executable .

chmod 700 test.sh

4. Now run the script test.sh

sh test.sh

OR 

./test.sh

OR

/GIVE/ABSOLUT/PATH/test.sh

Explaining the bash script

(a) read -p "Do you want to run command 'uname -ar' ? (yes/no)" reply

In this line, read command will get value and store the value in variable called ‘reply’.

(b) choice=$(echo $reply|sed 's/(.*)/L1/')

This line will convert all letters to small letters(see how we used sed command here). And new variable called ‘choice’ will keep the value of it.

(c) In rest of script we have used If..Else statement with nesting.

There are many ways you can create this kind of bash script as per your requirement.

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Password prompt in single user mode is not secure : CentOS/Red Hat
  2. Set and reset user password by bash script
  3. How to Use Expect In Bash Script
  4. How To Use case in Bash Script, Example
  5. How To Use opt In Bash Script
  6. send email after mysql backup through bash script in simple way
  7. Bash : print variable value inside single and double quotes
  8. Xen Server License Nagios Plugin (bash script)
  9. MySQL backup bash script
  10. Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Filed Under: Linux Tagged With: bash script

Reader Interactions

Comments

  1. Edward Crosby says

    June 27, 2016 at 8:04 pm

    Good post. This was exactly what I was looking for.
    But, what if I wanted this to run twice. I am trying to run a remote script to remote a server but on more than one server. Here is what I have attempted but it does not work as I get “syntax error: unexpected end of file”:

    read -p “Do you want to reboot the first server ? (yes/no)” reply

    choice=$(echo $reply|sed ‘s/(.*)/L1/’)

    if [ “$choice” = ‘yes’ ]
    then
    echo “You have selected to reboot the first server…”;
    sleep 3;
    ssh -t user@ip_address sh /home/user/scripts/reboot.sh;

    elif [ “$choice” = ‘no’ ]
    then

    echo “The server will not reboot”;
    sleep 3
    exit 0
    else
    read -p “Do you want to reboot the second server ? (yes/no)” reply

    choice=$(echo $reply|sed ‘s/(.*)/L1/’)

    if [ “$choice” = ‘yes’ ]
    then
    echo “You have selected to reboot the second server…”;
    sleep 3;
    ssh -t user@ip_address sh /home/user/scripts/reboot.sh;

    elif [ “$choice” = ‘no’ ]
    then

    echo “The server will not reboot”;
    sleep 3
    exit 0
    else
    echo “invalid answer, type yes or no”;
    fi

    • sharad chhetri says

      June 28, 2016 at 4:23 pm

      Hello Edward,

      I suspect in your script it may be due to some ASCII or hidden character you are getting error. Better to write the script and do not do copy paste. To debug your script I will suggest to use -x option while running the script. for eg. sh -x script_name.sh .
      In debug output carefully examine where exactly the script is stopped and showing error.

      Please try once.

      Regards
      Sharad

  2. Freddy says

    June 12, 2016 at 11:22 am

    Awesome!! Just what I needed, I’ve been searching for ages to find something to do this “choice=$(echo $reply|sed ‘s/(.*)/L1/’)” and make my if statement functional! Thanks ๐Ÿ™‚

    • sharad chhetri says

      June 12, 2016 at 2:15 pm

      You are most welcome Freddy!
      We are really happy to see your feedback. It will help others linux enthusiast.

      Regards
      Sharad

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy