Creating a Custom Greeting with Bash Scripting
Bash scripting is a powerful way to automate tasks in Unix-based systems. In this post, we'll create a simple Bash script that prompts the user for their name and age and then provides a personalized greeting with conditional messages based on their age.
What You’ll Need
A Unix/Linux environment
A text editor (like nano, vim, or any other)
Step-by-Step Guide
Open your terminal.
Create a new Bash script file. You can name it
greeting.sh.touch greeting.shOpen the file in your text editor.
nano greeting.shAdd the following code to the script:
#!/bin/bash # Prompt the user for their name read -p "Please enter your name: " name # Prompt the user for their age read -p "Please enter your age: " age # Conditional messages based on age if [ "$age" -lt 18 ]; then echo "Hello, $name! You're just a teenager!" elif [ "$age" -lt 65 ]; then echo "Hi, $name! You're in your prime!" else echo "Greetings, $name! You're a wise individual!" fiSave and exit the text editor. If you're using nano, press
CTRL + X, thenY, and hitEnter.Make the script executable:
chmod +x greeting.shRun the script:
./greeting.sh
How It Works
The script uses the
readcommand to take user input for their name and age.The
if,elif, andelsestatements check the age and print a corresponding message.
Conclusion
This simple script demonstrates how easy it is to use Bash for basic scripting tasks. You can expand this script by adding more conditions or functionalities, such as validating user input or adding more personalized messages.
Happy scripting!