πŸš€ Basic Linux Shell Scripting for DevOps Engineers

πŸš€ Basic Linux Shell Scripting for DevOps Engineers

Shell scripting is a must-have skill for DevOps engineers! It helps automate tasks, manage servers, and optimize workflows.

Β·

3 min read


πŸ–₯️ What is a Shell Script?

Imagine you own a pizza shop πŸ•, and every day you have to:

1️⃣ Open the shop πŸͺ
2️⃣ Turn on the oven πŸ”₯
3️⃣ Prepare the dough 🍞
4️⃣ Make pizza πŸ•
5️⃣ Serve customers πŸ›ŽοΈ
6️⃣ Clean the shop 🧹

Now, instead of doing this manually every single day, what if you could create a robot assistant πŸ€– that follows a list of instructions and does it for you?

That’s exactly what a shell script does for computers!

βœ… A shell script is like a to-do list πŸ“‹ for your computer.
βœ… It tells the computer what to do step by step.
βœ… It automates boring, repetitive tasks so you can relax. 😎


πŸ”Ή What is #!/bin/bash?

The first line of a shell script starts with shebang (#!), which tells the computer which language to use for the script.

#!/bin/bash
echo "Hello, DevOps!"

βœ”οΈ /bin/bash β†’ Uses the Bash shell (most common).
βœ”οΈ /bin/sh β†’ Uses a more general shell (might behave differently on some systems).

Think of it like telling your robot assistant which language to speak before giving instructions! πŸ€–


✍️ Shell Script to Print a Message

Let's create a simple script that prints a motivational message.

#!/bin/bash
echo "I will complete #90DaysOfDevOps challenge."

πŸ“Œ Save the file as challenge.sh and run:

chmod +x challenge.sh  # Give execute permission
./challenge.sh         # Run the script

Output:

I will complete #90DaysOfDevOps challenge.

πŸ”’ Script to Take User Input & Arguments

#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name!"

echo "Script Name: $0"
echo "First Argument: $1"
echo "Second Argument: $2"

πŸ“Œ How to run the script with arguments:

./script.sh DevOps Enthusiast

Output:

Enter your name:
Ana
Hello, Ana!
Script Name: script.sh
First Argument: DevOps
Second Argument: Enthusiast

Explanation:

  • $name β†’ Stores user input.

  • $0 β†’ The script name.

  • $1, $2, etc. β†’ Arguments passed when running the script.


πŸ”„ If-Else Statement in Shell Scripting

#!/bin/bash
num1=10
num2=20

if [ $num1 -gt $num2 ]
then
    echo "$num1 is greater than $num2"
else
    echo "$num1 is less than or equal to $num2"
fi

πŸ“Œ Comparison Operators:

  • -eq β†’ Equal to

  • -ne β†’ Not equal to

  • -gt β†’ Greater than

  • -lt β†’ Less than

  • -ge β†’ Greater than or equal to

  • -le β†’ Less than or equal to


πŸ” Loops in Shell Scripting

Loops help us repeat tasks automatically instead of writing the same commands multiple times.

1️⃣ For Loop πŸŒ€

A for loop is used when we know how many times we want to repeat something.

#!/bin/bash
for i in 1 2 3 4 5
do
  echo "Iteration number: $i"
done

πŸ“Œ Run this script, and it will print:

Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
Iteration number: 5

πŸ”Ή Real-life example:
Imagine you want to print 100 invoice copies. Instead of clicking print 100 times, a loop does it for you!

2️⃣ While Loop πŸ”„

A while loop runs until a condition is met.

#!/bin/bash
count=1

while [ $count -le 5 ]
do
  echo "Count: $count"
  count=$((count+1))  # Increase count by 1
done

πŸ“Œ Output:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

🌍 Real-Life Use Cases for Shell Scripting

βœ”οΈ πŸ“¦ Automate software deployment β†’ No more clicking buttons manually!
βœ”οΈ πŸ“Š Log Analysis β†’ Scan logs for errors and send alerts automatically.
βœ”οΈ ☁️ Cloud Automation β†’ Start/stop cloud servers when needed.
βœ”οΈ πŸ’Ύ Database Backups β†’ Save important data without forgetting.

Just like a robot assistant makes your life easier in the pizza shop, a shell script makes life easier for DevOps engineers!

Β