Blog 13 Boring to Pro: Making Shell Scripts Smarter with Functions, Colors & Logs”
Shell Scripting Made Human: Functions, Loops, Logging & Colors Explained with Stories
In the last blog, we covered the basics of shell scripting: variables, conditions, and simple commands. That’s like learning the ABCs of a new language. Now it’s time to move into making sentences and short stories. In scripting, that means functions, loops, logging, and adding some color to make our output lively 🎨.
1. Functions – Don’t Repeat Yourself (DRY)
Imagine you run a coffee shop ☕. Every customer who comes in asks for sugar in different amounts.
Instead of explaining to each barista again and again how to add sugar, you just write one clear instruction:
“Use add_sugar function.”
That’s what functions are in scripting – reusable instructions with a name. You write the code once and call it whenever you need.
Syntax
make_coffee() {
echo "Boiling water..."
echo "Adding coffee powder..."
echo "Pouring milk..."
echo "Coffee ready!"
}
# Call it
make_coffee
make_coffee
Notice how we can call make_coffee multiple times without rewriting all steps.
This saves time, reduces mistakes, and makes scripts easier to maintain.
Functions with Arguments
What if different customers want different types of coffee? Functions can accept inputs (arguments) just like humans take instructions.
make_coffee() {
echo "Making coffee with sugar level: $1"
}
make_coffee 2spoons
make_coffee 1spoon
Here, $1 is the first argument. The output changes based on what you pass in.
That’s how we make our scripts flexible, like a barista taking custom orders!
2. Adding Colors – Make Your Output Talk
Imagine a cricket scoreboard 🏏. If every message is in black text, it’s boring. But when “SIX!” flashes in green, or “OUT!” flashes in red, it immediately catches attention. Same with scripting – colors make your logs more readable.
Common Colors
- Green (32) → Success
- Red (31) → Errors
- Yellow (33) → Warnings
Example
echo -e "\e[32mSUCCESS: Package installed\e[0m" echo -e "\e[31mERROR: Installation failed\e[0m" echo -e "\e[33mWARNING: Package already exists\e[0m"
The code \e[32m turns text green, \e[31m turns text red, and \e[33m turns text yellow.
Finally, \e[0m resets the color back to normal.
3. Logging – Keeping a Diary for Your Script
Think about pilots ✈️. Every flight has a black box that records events. Why? Because if something goes wrong, we can check the logs and see what happened. Similarly, your script should keep a diary (logs) of everything it does.
Basic Logging
sh myscript.sh > myscript.log 2>&1
This command saves both success messages (1) and error messages (2) into a single file myscript.log.
Good Naming
Use meaningful names for logs, like:
install-packages.log13-logs-01-01-2025.log
Later, when you want to debug, these logs will be your best friend.
4. Loops – Work Smarter, Not Harder
Imagine you’re a teacher. Instead of calling out 50 student names one by one, you say: “Roll numbers 1 to 50, stand up.” 🎓 That’s what loops do in programming – they repeat tasks automatically.
Simple Loop
for i in {1..5}
do
echo "Number: $i"
done
This prints numbers 1 to 5. Simple, right?
Real-World Example: Installing Packages
for package in git mysql gcc nginx
do
echo "Installing $package..."
yum install -y $package
done
Instead of writing four separate install commands, one loop installs them all. This is how DevOps engineers save time (and avoid boredom).
5. Putting It All Together
Let’s combine functions, colors, logging, and loops into one mini-project. A script that installs multiple packages and logs results.
log_file="install-$(date +%F).log"
install_package() {
package=$1
if yum install -y $package &>>$log_file
then
echo -e "\e[32mSUCCESS: $package installed\e[0m"
elif [ $? -eq 1 ]
then
echo -e "\e[33mWARNING: $package already installed\e[0m"
else
echo -e "\e[31mERROR: Failed to install $package\e[0m"
fi
}
for pkg in git mysql gcc nginx
do
install_package $pkg
done
- Uses a function (install_package) to handle installation.
- Adds green for success, yellow for already installed, red for errors.
- Uses logging to save everything in a file.
- Loops over multiple packages automatically.
This is how you turn a plain script into a professional automation tool.
Final Thoughts
Shell scripting isn’t just about running commands. It’s about writing instructions that are clear, reusable, and reliable. Functions prevent repetition, loops save effort, colors make output easy to read, and logs give you history.
Think of it like running a kitchen 🍳 — you want recipes (functions), labels on ingredients (variables), multiple dishes prepared efficiently (loops), and a record of what was cooked (logs). That’s how professionals work in IT too.
Master these, and you’re not just writing scripts — you’re writing automation stories. 🚀
All rights reserved by Kalyan Kalavena. No copy or edit without permission.
Comments
Post a Comment