๐ DevSecOps Journey โ Day 9

Mastering Arrays, Command-Line Arguments, Error Handling & Regex in Shell Scripting
โจ Building smarter automation with reliable scripting practices
Todayโs session focused on expanding my Shell Scripting capabilities with arrays, argument handling, error management, and regular expressions. These skills are essential for writing dynamic, robust, production-ready DevOps automation.
๐งฉ 1. Working with Arrays in Shell
Arrays help store multiple values inside a single variableโuseful for handling lists of filenames, services, URLs, and more.
๐น What I learned today:
Add elements to an array
Delete elements
Loop through arrays
๐ Example:
fruits=("apple" "banana" "mango")
echo "${fruits[@]}"
# Add
fruits+=("orange")
# Delete (remove index 1)
unset fruits[1]
# Loop
for fruit in "${fruits[@]}"; do
echo "Fruit: $fruit"
done
๐งญ 2. Command-Line Arguments in Shell
Scripts become more powerful when they accept user inputs at runtime.
๐ง Key concepts:
$1,$2โ first & second argument$@โ all arguments$#โ number of arguments
๐ Example:
echo "First argument: $1"
echo "Total arguments: $#"
This is widely used in CI/CD scripts, deployment automation, and infrastructure tooling.
๐ก๏ธ 3. Error Handling & Management
A good script doesn't just workโit fails safely.
I explored how to detect, handle, and manage errors gracefully.
๐น Concepts practiced:
Checking exit codes (
$?)Detecting invalid input
Using conditions to prevent failures
Ensuring reliable execution flow
๐ Example:
cp file1.txt backup/
if [ $? -ne 0 ]; then
echo "Error: Copy failed!"
exit 1
fi
๐จ 4. Error Trapping with trap
The trap command allows capturing events like:
Script exit
Interrupt signals
Errors from commands
๐ Example:
trap "echo 'Something went wrong! Exiting safely...'" ERR
This is extremely useful in automation scripts to clean up temp files, stop services safely, or log failures.
๐ 5. Regular Expressions with grep, sed & awk
Regex is the backbone of text filtering, log processing, and data validation in DevOps.
๐น What I practiced:
grep โ search text using patterns
sed โ find & replace text
awk โ extract fields and process structured data
๐ Examples:
๐ธ grep โ filtering lines
grep -i "error" logfile.txt
๐ธ sed โ replacing text
sed 's/dev/prod/g' config.txt
๐ธ awk โ extracting column values
awk '{print $2,$4}' employees.txt
These tools are essential for:
โ๏ธ Parsing logs
โ๏ธ Validating configs
โ๏ธ Cleaning data
โ๏ธ Automation in CI/CD pipelines
๐ฏ Why Todayโs Learning Matters
These scripting concepts enable building automation that is:
More dynamic
More stable
Easier to debug
Scalable across environments
Reusable for multiple DevOps workflows
Mastering arrays, arguments, error control, and regex is a huge step toward writing production-ready automation.
โ Day 9 Summary
Today I learned:
๐น Arrays (add, delete, iterate)
๐น Command-line arguments (
$1,$@,$#)๐น Error handling & exit codes
๐น Error traps (
trap ERR)๐น Regex with
awk,sed,grep
These skills directly support real DevOps responsibilities such as:
โ๏ธ CI/CD scripting
โ๏ธ Log processing
โ๏ธ Deployment automation
โ๏ธ Configuration validation
โ๏ธ Infrastructure tasks
Excited to continue building stronger automation in the coming days!




