Creating Interactive Bash Scripts: A Beginner’s Guide to User Input and Conditional Statements

mdshamsfiroz
3 min readOct 30, 2024

--

Bash scripting is a powerful tool for automating tasks and creating interactive command-line applications. In this blog post, we’ll explore how to create a simple yet engaging Bash script that takes user input and responds with personalized messages. Let’s dive in!

Our Script’s Objective
We’ll create a script that:

  1. Asks for the user’s name
  2. Asks for the user’s age
  3. Greets the user by name
  4. Provides a custom message based on the user’s age

The CodeHere’s our Bash script:

#!/bin/bash
# Prompt for user's name and age
echo "Enter your name:"
read name
echo "Enter your age:"
read age
# Greet the user and provide a message based on age
if [ $age -lt 18 ]; then
echo "Hello, $name! You are a minor."
elif [ $age -ge 18 ] && [ $age -lt 65 ]; then
echo "Hello, $name! You are an adult."
else
echo "Hello, $name! You are a senior citizen."
fi

Let’s break down the script and understand each part:

  1. Shebang Line
    The first line #!/bin/bash tells the system to use Bash to interpret this script.
  2. User Input
    We use the echo command to prompt the user and the read command to capture their input into variables.
  3. Conditional Statements
    We use an if-elif-else structure to check the user's age and provide an appropriate message.
  4. Comparison Operators
  • -lt: less than
  • -ge: greater than or equal to

5. Logical Operators
We use && (AND) to combine conditions in the adult age range check.

Running the Script

To run this script:

  1. Save it to a file (e.g., greeting.sh)

2. Make it executable: chmod +x greeting.sh

3. Run it: ./greeting.sh

Enhancing the ScriptYou can expand this script in numerous ways:

  • Add more age categories with different messages
  • Include input validation to ensure the age is a number
  • Incorporate more personal details for a more tailored experience

Conclusion

This simple script demonstrates key concepts in Bash scripting:

  • Taking user input
  • Using variables
  • Implementing conditional logic

By mastering these basics, you can create more complex and useful scripts to automate tasks and interact with users in your command-line applications.
Remember, the power of Bash scripting lies in its simplicity and ubiquity in Unix-like systems. As you become more comfortable with these concepts, you’ll find countless opportunities to apply Bash scripts in your daily computing tasks and system administration duties.

So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.

Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”

Connect me on Social Media: https://linktr.ee/mdshamsfiroz

Happy coding! Happy learning!

Happy scripting!

--

--

mdshamsfiroz
mdshamsfiroz

Written by mdshamsfiroz

Trying to learn tool by putting heart inside to make something

No responses yet