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.

Read Some More Articles

4 Comments

  1. 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

    1. 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. 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 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.