☑️Day 11: Shell Scripting for Automation

☑️Day 11: Shell Scripting for Automation

🔹Table Of content :

  • ✅Introduction

  • ✅Key Learnings and Techniques

    • ✅If-Else and Elif Statements

      ✅Using For Loops to Automate Repetitive Tasks

      ✅Creating Files with One Command

      ✅Automating with Functions

      ✅Writing a DevOps Script to Take Backups

✅Takeaways from Day 11

✅What’s Next?


Hey DevOps Enthusiasts🚀,

Welcome to Day 11 of my #90DaysOfDevOps challenge📅! Today, I took a deep dive into advanced shell scripting, which has opened up even more possibilities for automating tasks and managing system processes.

Here’s a breakdown of the concepts I covered and the scripts I’ve written to automate DevOps tasks.


Key Learnings and Techniques

✅1. If-Else and Elif Statements

I learned how to control the flow of my scripts using if, elif, and else statements. These are essential when you need to execute different actions based on various conditions.

#!/bin/bash
echo "Enter a number:"
read number
if [ $number -gt 10 ]; then
    echo "The number is greater than 10."
elif [ $number -eq 10 ]; then
    echo "The number is exactly 10."
else
    echo "The number is less than 10."
fi

✅2. Using For Loops to Automate Repetitive Tasks

The for loop is a powerful tool for iterating over files, directories, or even ranges of numbers. Today, I used it to iterate through files with a .txt extension

for file in *.txt; do
    echo "Processing $file"
done

✅3. Creating Files with One Command

I also learned a cool trick to create multiple files at once using brace expansion:

touch file-{1..10}.txt

This command creates 10 files named file-1.txt, file-2.txt, all the way to file-10.txt.


✅4. Automating with Functions

Functions in shell scripting are crucial for automating tasks that need to be repeated with different inputs. I created an advanced function to add new users with predefined usernames and passwords.

add_user() {
    USER=$1
    PASS=$2
    useradd -m -p $PASS $USER && echo "Successfully added user: $USER"
}

#Main
add_user kedarP new@123

I ran this script using:

sudo ./adding_user.sh

I then verified the user was added by checking the /etc/passwd file:

cat /etc/passwd

✅5. Writing a DevOps Script to Take Backups

I also worked on writing a script to back up important files. However, I ran into a warning about removing leading '' from member names. I resolved this by using the --absolute-name option in the tar command.

Here’s the full command I used to create a backup:

src_dir=/home/ubuntu/scripts
tgt_dir=/home/ubuntu/backups

curr_timestamp=$(date "+%Y-%m-%d-%H-%M-%S")
backup_file=$tgt_dir/$curr_timestamp.tgz

echo "$curr_timestamp"

tar czf $backup_file --absolute-name $src_dir

echo "backup complete"

To extract the backup, I used:

tar xf backup.tgz

This backup and restore process will be a crucial part of my automation scripts moving forward.


✅Takeaways from Day 11

  • Conditional logic: I mastered using if-elif-else statements to handle decision-making in my scripts.

  • Looping: Loops are incredibly useful for iterating over files, especially when dealing with multiple instances of files like logs.

  • Functions: I can now create reusable functions, simplifying tasks like user management.

  • Backup Automation: Writing backup scripts with tar and handling real-world issues like path warnings gave me a practical approach to system administration


✅What’s Next?

Next, I’ll dive deeper into task automation and explore how shell scripting integrates with other DevOps tools. It’s all about enhancing efficiency and taking automation to the next level.

🚀Stay tuned for more updates on my DevOps journey! 📅

Feel free to reach out to me if you have any questions or need any more help. Let’s connect, learn, and succeed together!

Thanks for joining me on Day 11 of this exciting journey. Let’s keep leveling up our skills, one productive hour at a time!

Happy Learning!😊

#90DaysOfDevOps

#Linux #ShellScripting #Automation #DevOpsJourney #TaskAutomation #BashScripting

💡
Follow for more updates on LinkedIn , GitHub and Twitter(X)