Learn Shell Scripting: A Simple Guide to Loops, Functions, and Automation


Shell scripting is a powerful tool for automating repetitive tasks in Linux. Whether you're processing files, checking connectivity, or scheduling jobs, mastering loops, functions, and automation can make your scripts efficient and robust.
In this blog, we'll cover:
Different types of loops (
for
,while
,until
, and infinite loops)Reading files with loops
Functions and argument passing
Useful scripting concepts (variables, redirection, logging, debugging)
Automation using
at
andcron
Let’s dive in!
1. For Loop in Shell Script
The for
loop iterates over a list of values.
Syntax:
for var in value1 value2 value3; do
echo "$var"
done
Example:
for i in 1 2 3 4 5; do
echo "Number: $i"
done
2. For Loop to Get Values from a File
You can read lines from a file using a for
loop.
Example:
for line in $(cat file.txt); do
echo "$line"
done
Better Approach (handles spaces correctly):
while IFS= read -r line; do
echo "$line"
done < file.txt
3. While Loop in Shell Script
The while
loop runs as long as a condition is true.
Syntax:
while [ condition ]; do
commands
done
Example (Countdown):
count=5
while [ $count -gt 0 ]; do
echo "Countdown: $count"
((count--))
done
4. Until Loop in Shell Script
The until
loop runs until a condition becomes true (opposite of while
).
Syntax:
until [ condition ]; do
commands
done
Example:
count=1
until [ $count -gt 5 ]; do
echo "Count: $count"
((count++))
done
5. Infinite Loop in Shell Script
An infinite loop runs forever until manually stopped (Ctrl+C
).
Using while
:
while true; do
echo "Infinite loop running..."
sleep 1
done
Using for
:
for (( ; ; )); do
echo "Infinite loop"
sleep 1
done
6. While Loop with File
You can read a file line-by-line using while
.
Example:
while IFS= read -r line; do
echo "Line: $line"
done < file.txt
7. Functions in Shell Script
Functions help in reusing code.
Syntax:
function_name() {
commands
}
Example:
greet() {
echo "Hello, $1!"
}
greet "Alice"
8. Passing Arguments in Shell Script
You can pass arguments to scripts or functions.
Script Arguments:
echo "First arg: $1"
echo "Second arg: $2"
Function Arguments:
sum() {
echo $(($1 + $2))
}
sum 5 10
9. Other Useful Concepts
BASH Variables
name="John"
echo "Hello, $name"
Redirection (>
, >>
, 2>
, &>
)
>
– Overwrite file>>
– Append to file2>
– Redirect errors&>
– Redirect both output and errors
/dev/null in Shell Script
Discard output:
command > /dev/null # Suppress output
command 2> /dev/null # Suppress errors
Logging in Shell Script
logfile="script.log"
echo "$(date): Script started" >> "$logfile"
Debugging in Shell Script
#!/bin/bash -x # Enable debug mode
set -x # Start debugging
set +x # Stop debugging
10. Connectivity Check Script
Check if a host is reachable:
ping -c 1 google.com &> /dev/null
if [ $? -eq 0 ]; then
echo "Host is reachable."
else
echo "Host is down."
fi
11. Automate Script Using at
Command
Run a script once at a specific time:
echo "/path/to/script.sh" | at 10:00 PM
12. Automate Script Using Crontab
Schedule recurring tasks with cron
:
Edit Crontab:
crontab -e
Example (Run daily at 5 AM):
0 5 * * * /path/to/script.sh >> /path/to/logfile.log
Conclusion
Shell scripting is essential for automation in Linux. By mastering loops, functions, and scheduling tools like cron
and at
, you can streamline repetitive tasks and improve efficiency.
Try implementing these concepts in your scripts and automate your workflows today! 🚀
Subscribe to my newsletter
Read articles from Sdeep directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sdeep
Sdeep
👋 Hello! I'm passionate about DevOps and I'm proficient in a variety of cutting-edge technologies and always motivated to expand my knowledge and skills. Let's connect and grow together!