How to prompt for Yes/No/Cancel input in a Linux shell script
Last Updated :
14 Nov, 2022
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:
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.
Similar Reads
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to Run a Shell Script in Linux
Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently. In this guide, we'll show the steps to check a shell script in Linux before running it,
4 min read
Bash shell script to find if a number is perfect or not
In this article, we will discuss how to write a bash script to find if a number is perfect or not. A perfect number is defined as, a positive number that is equal to the sum of its proper divisors. Smallest no is 6 ex= 1,2,3 are divisor of 6 and 1+2+3=6 Method 1: Using While LoopRead the input using
3 min read
How to Enable Shell Script Debugging Mode in Linux?
Linux is a very popular operating system especially among developers and in the computer science world, as it comes along with its powerful ability to play with the kernel. Major Linux distributions (often referred to as Linux distros) are Open Source (Source Code available and can be modified on re
6 min read
Shell Script to Perform Operations on a File
Most of the time, we use shell scripting to interact with the files. Shell scripting offers some operators as well as some commands to check and perform different properties and functionalities associated with the file. For our convenience, we create a file named 'geeks.txt' and another .sh file (or
5 min read
Shell Script To Check For a Valid Floating-Point Value
User Input can be hectic to manage especially if it's about numbers. Let's say you wanted to perform mathematical calculations in your app or script. It's not hard to say that it will have to deal with floating-point numbers. It's easy in many statically typed programming languages but if it has to
4 min read
Shell Script to Demonstrate Wait Command in Linux
Wait command is one of the process management commands. There are different process commands in Linux mainly 5 commands are widely used which are ps, wait, sleep, kill, exit. ps is an acronym for process status. It displays information about the active processes. wait command will suspend execution
4 min read
Shell Script Program to Convert Image Formats
Image Format Conversion is one of the most common processes in different fields like Web Development, Graphic Design, etc. As in Linux Environment, we have a Shell Scripting tool that can be used to automate the process of converting the image formats in a couple of seconds. We can convert the image
9 min read
How to Print Second Command line Argument in shell?
Command line arguments are the parameters we pass to a command or script when we run it from the terminal. These arguments are passed as strings and can be accessed within your program to perform various operations. How to Print Second Command Line Argument in Shell?The shell stores the command line
5 min read
How to Execute Shell Commands in a Remote Machine in Python?
Running shell commands on a Remote machine is nothing but executing shell commands on another machine and as another user across a computer network. There will be a master machine from which command can be sent and one or more slave machines that execute the received commands. Getting Started We wi
3 min read