How do I prompt for Yes/No/Cancel input in a Linux shell script?

Cover Image for How do I prompt for Yes/No/Cancel input in a Linux shell script?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Prompt for Yes/No/Cancel Input in a Linux Shell Script 🤔💻

Have you ever found yourself needing to ask the user for a simple yes, no, or cancel input in a Linux shell script? It's a common situation when you want to create an interactive script or automate certain tasks. In this blog post, we'll explore different methods to achieve this goal and provide you with easy solutions. Let's dive in! 🚀

The Challenge: Pausing Input and Prompting for Choices

To start, let's understand the challenge at hand. You want to pause the execution of your shell script momentarily and prompt the user with a standard "yes," "no," or "cancel" type question. The goal is to obtain the user's input and act accordingly in your script.

Solution #1: Using the read Command

The simplest way to achieve this is by using the read command. Here's how you can prompt the user and obtain their input in a Linux shell script:

echo "Do you want to proceed? (Y/N/C)"
read choice

case $choice in
  y|Y)
    echo "User chose 'Yes'";;
  n|N)
    echo "User chose 'No'";;
  c|C)
    echo "User chose 'Cancel'";;
  *)
    echo "Invalid choice";;
esac

Let's break it down:

  1. The echo command is used to display the question to the user.

  2. The read command waits for the user to input their choice and stores it in the choice variable.

  3. The case statement checks the value of choice and executes the corresponding code block based on the user's input.

This method allows you to handle multiple user choices and take different actions accordingly.

Solution #2: Utilizing the select Command

Another option that provides a more interactive experience is to use the select command. It creates a simple menu system for the user to choose from. Here's an example:

echo "Select your choice:"
select choice in "Yes" "No" "Cancel" "Quit"; do
  case $choice in
    Yes)
      echo "User chose 'Yes'"
      break;;
    No) 
      echo "User chose 'No'"
      break;;
    Cancel)
      echo "User chose 'Cancel'"
      break;;
    Quit)
      echo "Quitting..."
      exit;;
    *)
      echo "Invalid choice";;
  esac
done

Here's what's happening:

  1. The echo command displays the prompt to the user.

  2. The select statement creates a menu based on the options provided.

  3. The case statement determines the action to execute based on the user's choice.

  4. The break statement exits the loop after the user makes their selection, ensuring the script doesn't keep asking for input.

Feel free to extend the menu options or modify the code to fit your specific needs.

Engage with the Community! 🤝

We've covered two simple solutions to prompt for Yes/No/Cancel input in a Linux shell script. Now it's your turn! 🎉

Have you encountered this challenge before? How did you solve it? Do you have any alternative methods? Let us know by leaving a comment below. Sharing your experiences and insights helps others in the community. Together, we can create more efficient and user-friendly shell scripts! 💪💡

Happy scripting! 💻✨


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello