Greeting Bash Script
Introduction
This Bash script is designed to take user input (name and age) and display a custom greeting based on the user’s age. The script demonstrates simple user interaction, input handling, and conditional logic in Bash, making it a great example of using basic Bash scripting commands.
Code Explanation
#!/bin/bash
read -p "Enter your name: " name
read -p "Enter your age: " age
echo "Hello, $name!"
if (( age < 18 )); then
echo "You're a minor. Enjoy your youth!"
elif (( age >= 18 && age <= 65 )); then
echo "You're an adult. Keep striving for your goals!"
else
echo "You're a senior citizen. Wisdom comes with experience!"
fi
Script Header (
#!/bin/bash
): Defines the interpreter to use for the script, which is Bash.Name Input:
read -p "Enter your name: " name
prompts the user to enter their name, storing it in thename
variable.Age Input:
read -p "Enter your age: " age
prompts the user to enter their age, storing it in theage
variable.Conditional Logic:
if (( age < 18 ))
: Checks if the age is less than 18 and displays a message for minors.elif (( age >= 18 && age <= 65 ))
: Checks if the age is between 18 and 65, displaying an adult message.else
: Catches any age over 65 and displays a message for senior citizens.
Usage
Save the script as
greeting.sh
.Make the script executable by running:
chmod +x greeting.sh
Run the script:
./greeting.sh
Expected Output
Upon running, the script will prompt the user for their name and age, and then display a personalized greeting based on the age input. Here’s an example:
Enter your name: Aman
Enter your age: 21
Hello, Aman!
You're an adult. Keep striving for your goals!
Sample Outputs Based on Age
If age < 18:
Hello, [Name]! You're a minor. Enjoy your youth!
If 18 <= age <= 65:
Hello, [Name]! You're an adult. Keep striving for your goals!
If age > 65:
Hello, [Name]! You're a senior citizen. Wisdom comes with experience!
Conclusion
This script provides a straightforward example of user interaction and conditional logic in Bash. By prompting the user for their name and age, the script offers personalized greetings, highlighting the flexibility and power of Bash scripting for creating custom user experiences based on input.