Prompting for Yes/No/Cancel Input in Linux Shell Scripts

In Linux shell scripting, it’s often necessary to prompt users for input and validate their responses. One common scenario is asking a yes/no/cancel question, where the user must respond with one of these options. In this tutorial, we’ll explore how to achieve this using various methods in Bash.

Using the read Command

The read command is a fundamental way to get user input in a shell script. We can use it in conjunction with a while loop and a case statement to handle yes/no/cancel responses. Here’s an example:

while true; do
    read -p "Do you wish to install this program? " yn
    case $yn in
        [Yy]* ) make install; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

In this code, the read command prompts the user with a question and stores their response in the $yn variable. The case statement then checks the value of $yn and performs the corresponding action.

Using the select Command

Another way to prompt for yes/no/cancel input is by using the select command, which displays a menu of options and allows the user to choose one. Here’s an example:

echo "Do you wish to install this program?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) make install; break;;
        No ) exit;;
    esac
done

The select command is more concise and user-friendly than the read method, as it displays the available options and allows the user to select one by entering a number.

Handling Flexible Input

If you want to allow users to enter their responses in different formats (e.g., "yes", "Yes", "y"), you can modify the select command to handle this. Here’s an example:

echo "Do you wish to install this program?"
select strictreply in "Yes" "No"; do
    relaxedreply=${strictreply:-$REPLY}
    case $relaxedreply in
        Yes | yes | y ) make install; break;;
        No  | no  | n ) exit;;
    esac
done

In this code, the relaxedreply variable is used to store the user’s response in a more flexible format.

Making Your Script Language-Agnostic

If you want your script to be usable by users who speak different languages, you can use the locale command to determine the user’s language and adjust the prompt accordingly. Here’s an example:

set -- $(locale LC_MESSAGES)
yesexpr="$1"; noexpr="$2"; yesword="$3"; noword="$4"

while true; do
    read -p "Install (${yesword} / ${noword})? " yn
    if [[ "$yn" =~ $yesexpr ]]; then make install; exit; fi
    if [[ "$yn" =~ $noexpr ]]; then exit; fi
    echo "Answer ${yesword} / ${noword}."
done

This code uses the locale command to determine the user’s language and sets variables for the yes/no expressions and words. The prompt is then adjusted accordingly.

In conclusion, prompting for yes/no/cancel input in Linux shell scripts can be achieved using various methods, including the read and select commands. By handling flexible input and making your script language-agnostic, you can create more user-friendly and accessible scripts.

Leave a Reply

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