# Advanced Bash Scripting

## Introduction

Continuing my DevOps journey, today I went deeper into **Bash scripting**, focusing on advanced concepts like functions, automation, and task scheduling. Mastering these skills is essential for automating system administration tasks, managing servers, and improving workflow efficiency.

## What I Learned Today?

### 1️⃣ Functions in Bash

Functions help in code reusability and modular scripting. I practiced:

* Defining functions:
    
    ```bash
    my_function() {
        echo "Hello, this is a function!"
    }
    my_function
    ```
    
* Passing arguments to functions:
    
    ```bash
    greet_user() {
        echo "Hello, $1! Welcome to Bash scripting."
    }
    greet_user "Pratham"
    ```
    
* Using return values:
    
    ```bash
    sum() {
        result=$(( $1 + $2 ))
        echo $result
    }
    total=$(sum 5 10)
    echo "Total: $total"
    ```
    

### 2️⃣ Process Management & Background Jobs

Understanding how to manage processes is essential for handling system performance efficiently. I practiced:

* Running scripts in the background:
    
    ```bash
    ./script.sh &
    ```
    
* Checking running processes:
    
    ```bash
    ps aux | grep script.sh
    ```
    
* Killing a process:
    
    ```bash
    kill <process_id>
    ```
    

### 3️⃣ Log Management & Debugging

Debugging Bash scripts is crucial to avoid unexpected issues. I explored:

* Debugging with `set -x`:
    
    ```bash
    #!/bin/bash
    set -x
    echo "Debugging mode enabled!"
    ```
    
* Using `trap` to handle errors gracefully:
    
    ```bash
    trap 'echo "An error occurred. Exiting..."; exit 1' ERR
    ```
    
* Log rotation for system logs:
    
    ```bash
    logrotate /etc/logrotate.conf
    ```
    

## Challenges I Faced

* Understanding function return values in Bash.
    
* Debugging unexpected script failures.
    

## What’s Next?

Tomorrow, I will focus on:

* **Shell scripting for system administration**
    
* **Working with APIs using** `curl` and `wget`
    

Bash scripting is an essential skill for DevOps engineers, and today’s practice strengthened my understanding of automation, process management, and debugging techniques. The journey continues!

3/n

#DevOps #BashScripting #Automation #100DaysOfDevOps
