π 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.
Table of contents
π₯οΈ 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!