Open In App

How to prompt for Yes/No/Cancel input in a Linux shell script

Last Updated : 14 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

You may have noticed that shell programs occasionally ask the user for confirmation by prompting [Y/n] or [Yes/No]. Knowing whether a user wishes to continue with the following stages or not is useful. A similar feature can be added to your script as well. This article will assist you with examples of this sort of input (Bash Script – Prompt to Confirm (Y/N, YES/NO)). You may have noticed that shell programs occasionally ask the user for confirmation by prompting [Y/n] or [Yes/No]. Knowing whether a user wishes to continue with the following stages or not is useful. A similar feature can be added to your script as well.

In this article, we will discuss how to prompt for user input for the following choices in a Linux shell script:

  • Yes (Y)
  • No(N)
  • Cancel

This task is fairly easy to create using a shell script and we shall how the same could be done for various situations. To prompt the input, we will use the read function of shell scripting and then use the case statements for performing those various choices.

We can simply get user input from the read command in BASH. It provides a lot of options and arguments it for more flexible usage, but we’ll cover them in the next few sections. For now, let’s see how a basic read command can be used.

Syntax of read function:

read -[option] variable

There are numerous options that can be supplied to the read command, allowing for customizable user input collection. Here, some of the few reasons are covered: 

  • Prompt String (-p)
  • Password Input (-s)
  • Changing the Delimiter (IFS)
  • Parsing to the array (-a)
  • Limiting Length of the Input (-n)
  • Timed Input (-t)

Syntax of the case statements:

case "choice" in 
option 1) <statements
.
.
*) default case
esac

Now let us see the usage of these in implementation for taking user choices in various examples.

Example 1: Install a Software

Let us create a script where we ask the user to install software, with the choices “yes”, “no”, and “cancel”.

Script:

#!/bin/bash

# prompting for choice
read -p "Do you want to install bc calculator. (y)Yes/(n)No/(c)Cancel:- " choice

# giving choices there tasks using
case $choice in
[yY]* ) echo "Making dummy install" ;;
[nN]* ) echo "Thanks for using the script" ;;
[cC]* ) echo "Installation cancelled" ;;
*) exit ;;
esac

This script will accept both Upper case and Lower-case initials for the three choices. The output is:

Output:

After executing the script, the user is prompted to enter his/her choice with 3 different options:- (y)Yes/(n)No/(c)Cancel. if the user enters (y) then the software starts its installation process, if the user enters (no) then the script is terminated and if the user enters (cancel) then the installation process is been terminated or canceled.

 

Example 2: Using if-else block

Let us take choices and execute them with the if-else block.

Script:

#!/bin/bash
read -p "Enter your choice. Yes(y) / No(n) / Cancel(c):- " choice
if [ "$choice" = "y" ]; then 
echo "Making the dummy install!" 
elif [ "$choice" = "n" ];then
echo "Making no install"
elif [ "$choice" = "c" ];then
echo "Installation cancelled"
else 
echo "Wrong!"
fi

Here we use the string comparison using the ‘=’ operator for matching the input choices.

Output:

Here, instead of using switch case blocks, we have used if-else statements for performing actions on software installation through the input given by the user.

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads