Advanced Bash Scripting on AlmaLinux

It is time to move beyond the basics of bash scripting and explore advanced techniques.

Bob realized that while he could perform many tasks manually, scripting would allow him to automate repetitive jobs, reduce errors, and save time. It was time to move beyond the basics of bash scripting and explore advanced techniques.

“With great scripts comes great power!” Bob said, excited to unlock the full potential of bash.


Chapter Outline: “Bob Delves into Advanced Bash Scripting”

  1. Introduction: Why Bash Scripting?

    • Advantages of automation in system administration.
    • Recap of bash scripting basics.
  2. Using Functions in Scripts

    • Defining and calling functions.
    • Using functions for modular code.
  3. Working with Arrays

    • Declaring and accessing arrays.
    • Using loops to process array elements.
  4. Error Handling and Debugging

    • Checking command success with $?.
    • Debugging scripts with set -x.
  5. Advanced Input and Output

    • Redirecting output and appending to files.
    • Using read for interactive scripts.
  6. Text Processing with awk and sed

    • Transforming text with awk.
    • Editing files in-place with sed.
  7. Creating Cron-Compatible Scripts

    • Writing scripts for cron jobs.
    • Ensuring scripts run reliably.
  8. Conclusion: Bob Reflects on Scripting Mastery


Part 1: Introduction: Why Bash Scripting?

Bob understood that bash scripting is the glue that holds system administration tasks together. From automating backups to monitoring servers, scripts are indispensable tools for any sysadmin.

Recap of Bash Basics

  • Writing a script:

    #!/bin/bash
    echo "Hello, AlmaLinux!"
    
  • Making it executable:

    chmod +x myscript.sh
    

“Time to level up and make my scripts smarter!” Bob said.


Part 2: Using Functions in Scripts

Functions help Bob organize his scripts into reusable chunks of code.

Step 1: Defining and Calling Functions

Bob created a simple function to check if a service was running:

#!/bin/bash

check_service() {
    if systemctl is-active --quiet $1; then
        echo "$1 is running."
    else
        echo "$1 is not running."
    fi
}

check_service httpd

Step 2: Passing Arguments to Functions

Functions can accept arguments:

#!/bin/bash

greet_user() {
    echo "Hello, $1! Welcome to $2."
}

greet_user "Bob" "AlmaLinux"

“Functions make my scripts modular and readable!” Bob noted.


Part 3: Working with Arrays

Bob learned to use arrays to store and process multiple values.

Step 1: Declaring and Accessing Arrays

  • Declare an array:

    services=("httpd" "sshd" "firewalld")
    
  • Access elements:

    echo ${services[0]}  # Outputs: httpd
    

Step 2: Looping Through Arrays

Bob wrote a script to check the status of multiple services:

#!/bin/bash

services=("httpd" "sshd" "firewalld")

for service in "${services[@]}"; do
    systemctl is-active --quiet $service && echo "$service is running." || echo "$service is not running."
done

“Arrays are perfect for handling lists of items!” Bob said.


Part 4: Error Handling and Debugging

Bob added error handling to his scripts to catch failures gracefully.

Step 1: Checking Command Success

The $? variable stores the exit status of the last command:

#!/bin/bash

mkdir /tmp/testdir
if [ $? -eq 0 ]; then
    echo "Directory created successfully."
else
    echo "Failed to create directory."
fi

Step 2: Debugging with set -x

Bob used set -x to debug his scripts:

#!/bin/bash
set -x
echo "Debugging this script."
mkdir /tmp/testdir
set +x

“With error handling and debugging, my scripts are rock solid!” Bob said.


Part 5: Advanced Input and Output

Bob explored advanced ways to handle input and output in his scripts.

Step 1: Redirecting Output

  • Redirect standard output:

    ls > filelist.txt
    
  • Redirect standard error:

    ls /nonexistent 2> error.log
    

Step 2: Using read for Interactive Scripts

Bob wrote a script to prompt for user input:

#!/bin/bash

read -p "Enter your name: " name
echo "Hello, $name!"

“Interactive scripts make user input seamless!” Bob said.


Part 6: Text Processing with awk and sed

Bob enhanced his scripts with powerful text-processing tools.

Step 1: Using awk

Bob used awk to extract specific columns from a file:

#!/bin/bash

echo -e "Name Age Bob 30 Alice 25" > users.txt
awk '{print $1}' users.txt  # Outputs: Name, Bob, Alice

Step 2: Editing Files with sed

Bob used sed to perform in-place edits:

#!/bin/bash

echo "Hello World" > message.txt
sed -i 's/World/Bob/' message.txt

“With awk and sed, I can transform data like a pro!” Bob said.


Part 7: Creating Cron-Compatible Scripts

Bob learned to write scripts that run reliably as cron jobs.

Step 1: Writing a Cron-Compatible Script

Bob created a script to back up logs:

#!/bin/bash

tar -czf /backup/logs-$(date +%F).tar.gz /var/log
  • Add it to the crontab:

    crontab -e
    

    Add the following line to run the script daily at midnight:

    0 0 * * * /home/bob/backup_logs.sh
    

Step 2: Testing Cron Jobs

Bob tested the script manually to ensure it worked:

bash /home/bob/backup_logs.sh

“Automation for the win—cron jobs save me so much time!” Bob said.


Conclusion: Bob Reflects on Scripting Mastery

Bob now had the skills to write advanced bash scripts that were modular, reliable, and powerful. Armed with these tools, he felt ready to tackle any system administration challenge.

Next, Bob plans to explore SELinux Policies and Troubleshooting on AlmaLinux.