Day 4: Intro to Linux Shell Scripting for DevOps Beginners

Day 4: Intro to Linux Shell Scripting for DevOps Beginners

ยท

3 min read

Hello, DevOps enthusiasts! ๐Ÿ‘‹

On Day 04 of my #90DaysOfDevOps journey, I explored Basic Linux Shell Scripting and its importance in the life of a DevOps engineer. Shell scripting is an essential tool for automating processes, and this session introduced me to writing scripts for various tasks. Letโ€™s dive into what I learned and accomplished today!


๐Ÿ› ๏ธ Tasks and Solutions

Task 1: What is Shell Scripting?

Shell scripting involves writing a series of commands in a script file to automate tasks in a Unix/Linux environment.

Why is it important for DevOps?

  • Automates repetitive tasks.

  • Simplifies system management and configurations.

  • Speeds up CI/CD pipelines.

  • Reduces manual errors, saving time and effort.

Example: A script to check the disk usage of a system:

#!/bin/bash
df -h > disk_usage_report.txt
echo "Disk usage report saved to disk_usage_report.txt"

Task 2: What is #!/bin/bash? Can we write #!/bin/sh as well?

The shebang line (#!) indicates the interpreter to execute the script.

  • #!/bin/bash: Uses Bash, which supports advanced features like arrays and functions.

  • #!/bin/sh: Uses the Bourne shell, more compatible with POSIX-compliant systems.

Example Difference:

#!/bin/bash
echo ${BASH_VERSION}  # Only works in Bash

#!/bin/sh
echo "POSIX-compliant script"

Task 3: Write a Shell Script to Print a Message

Script:

#!/bin/bash


<<about

Author : Amitabh Soni
Date : 23/11/24
Description : This shell script will print I will complete #90DaysOfDevOps challenge

about


echo "I will complete #90DaysOfDevOps challenge"

Execution Output:


Task 4: Script to Take User Input and Arguments

Script:

#!/bin/bash

<<Info
Author : Amitabh soni
Date : 23/11/24
Description : This scripts takes input from user and print it
Info

# userName
read -p "Enter your name : " name

# storing arguments
arg1=$1
arg2=$2

# Printing name and passed arguments
echo "Your name is ${name} and arguments passed : ${arg1} , ${arg2}."

Execution:


Task 5: If-Else Statement Comparing Two Numbers

Script:

#!/bin/bash

<<info
Author : Amitabh soni
Date : 23/11/24
Description : This script will compare two numbers using If-else statements
info

# user input
read -p "Enter number 1 : " n1
read -p "Enter number 2 : " n2

# Actual program
if [[ $n1 -gt $n2 ]]; then
        echo "$n1 is greater than $n2"
elif [[ $n1 -lt $n2 ]];then
        echo "$n2 is greater than $n1"
else
        echo "$n1 is equal to $n2"
fi

Execution Output:


๐ŸŽ‰ Takeaway

Todayโ€™s tasks gave me a solid foundation in Linux shell scripting, which is crucial for automating and optimizing processes in DevOps workflows. Iโ€™m excited to keep building on this knowledge in the upcoming days.

You can find my solutions and scripts here: GitHub Repository

Let me know your thoughts and feedback in the comments below! ๐Ÿš€

#DevOps #Linux #ShellScripting #Automation #LearningJourney

ย