Start From Linux with Shell Scripting for DevOps :
Shell scripting is like learning a new language — at first, the syntax feels cryptic, but every small win builds confidence. Over time, I’ve discovered not just commands, but ways of thinking about automation, problem solving, and DevOps workflows. Here’s a reflection on the lessons, challenges, and insights I’ve gathered so far.
Early Lessons Learned
File and Directory Operations
Use find when you need to search across directories.
Use [ -f ] when you already know the exact path you want to check.
:> filename → resets a file to empty without deleting it.
exit 1 → stops the script at the failure point.
echo $? → inspects the exit code of the last command.
👉 These basics taught me how to control flow and handle files safely.-
Conditional Logic
[ ] → used for conditions (strings, files, etc.).
[[ ]] → more powerful, supports pattern matching.
(( )) → used for arithmetic comparisons.
Commands can run directly in if without [ ]./dev/null → suppresses normal output.
2>&1 → suppresses error messages too.
Challenge: Forgetting spaces inside [ ] caused errors like “command not found”. Lesson: Always put spaces around brackets.
🔄 Loops: Thinking in Iterations
🛠️ How to Think About Loops
For loop → “Do this for each item.” (fixed list or range).
While loop → “Keep doing this until the condition fails.”
Until loop → “Keep doing this until the condition succeeds.”
Array loop → Use "${array[@]}" with for because you know the items.
🔄 Types of Loops in Shell
- For Loop
for i in {1..5}
do
echo "Number: $i"
done
Use case: Iterating over files in a directory.
for file in *.txt
do
echo "Processing $file"
done
- While Loop
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
count=$((count+1))
done
Use case: Monitoring a service until it starts.
- Until Loop
bash
count=1
until [ $count -gt 5 ]
do
echo "Count: $count"
count=$((count+1))
done
- Infinite Loop
while true
do
echo "Running..."
sleep 2
done
Use case: Continuous monitoring tasks.
Rule of Thumb
Inside loops:
for file in *.sh → no need for [[ ]].
Use [[ ]] for string/pattern conditions.
Use (( )) for numeric conditions.
Outside loops: same rule applies.
Best Example:
#!/bin/bash
for file in *
do
if [[ $file == *.sh ]]; then
echo "Shell script: $file"
fi
done
⚙️ DevOps Connection
For loop + [[ ]] → Iterate files and check extensions, permissions, or patterns.
For loop + (( )) → Iterate numbers (like retries, ports, IDs).
While loop → Monitor service status or retry logic.
Until loop → Wait until a condition succeeds (e.g., deployment ready).
🧩 Challenges We Faced
Parsing errors: Forgetting spaces in [ ].
Percentage vs actual size: df -h vs du -sh.
Suppressing output: Learning >/dev/null 2>&1.
Exit codes: Understanding how echo $? helps debug scripts.
✅ Key Takeaways
Think in conditions: [ ] for files/strings, [[ ]] for patterns, (( )) for numbers.
Think in loops: For known ranges → for; for monitoring → while; for waiting → until.
Normalize values: Use numfmt to convert human-readable disk sizes into integers.
Fail fast: Use exit 1 to stop at the failure point.
Debug smartly: Use echo $? to check exit codes.
Top comments (0)