Skip to main content

Command Palette

Search for a command to run...

๐Ÿš€ DevSecOps Journey โ€” Day 9

Published
โ€ข3 min read
๐Ÿš€ 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!

More from this blog

D

DevSecOps - Zero To Hero

19 posts