DEV Community

Cover image for Day 4: Basic Linux Shell Scripting for DevOps Engineers
Udoh Deborah
Udoh Deborah

Posted on

Day 4: Basic Linux Shell Scripting for DevOps Engineers

What is the Kernel?
The kernel is the core of an operating system. It acts as a bridge between software and hardware, managing system resources like CPU, memory, and devices. Think of it as the brain of your computer, ensuring everything runs smoothly behind the scenes.

What is the Shell?
The shell is a command-line interface (CLI) that allows users to communicate with the operating system. It serves as a translator — converting human-readable commands into instructions the kernel can understand and execute.

What is Linux Shell Scripting?
Shell scripting involves writing a sequence of commands in a script file that can be executed by the shell.
It’s like creating a recipe for the computer — a set of instructions it follows to perform specific tasks, from simple file operations to complex system automation.

What is Shell Scripting for DevOps?
In DevOps, shell scripting plays a vital role in automation, configuration, deployment, and monitoring. It helps engineers streamline workflows and reduce manual effort

Real-World Examples:
✅ Automated Deployment
Instead of manually deploying apps to multiple servers, use a shell script to handle it in seconds.

✅ Infrastructure Setup
Automate the creation of virtual machines or cloud environments using scripts, saving time and ensuring consistency.

✅ Backups & Maintenance
Write scripts to back up databases or clean up logs regularly, reducing risks of data loss and system bloat.

✅ CI/CD Pipelines
Shell scripts help power continuous integration and deployment, automating testing, building, and deploying code with each commit.

✅ System Monitoring
Scripts can monitor disk space, CPU usage, or running processes — and send alerts if thresholds are exceeded, enabling proactive issue resolution.

Final Thought:
Shell scripting transforms DevOps from manual to automated, repeatable, and reliable — the key to building scalable systems.

Here are some examples of Simple Script

  • Instead of typing this every time:

bash

mkdir myapp
cd myapp
touch app.sh
Enter fullscreen mode Exit fullscreen mode

You can write it once in a file called setup.sh:

bash

#!/bin/bash
mkdir myapp
cd myapp
touch app.sh
Enter fullscreen mode Exit fullscreen mode

Then run it with:

bash
bash setup.sh

Enter fullscreen mode Exit fullscreen mode

And there you go! You’ve just written and executed your first shell script.

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

#!/bin/bash:
Enter fullscreen mode Exit fullscreen mode

This line is called a shebang.

It tells the system to run the script using the Bash shell.

#!/bin/sh:
Enter fullscreen mode Exit fullscreen mode

Yes, you can use this too.

It uses the sh shell (simpler, more portable, but with fewer features than bash).

Use #!/bin/bash when you want full bash features. Use #!/bin/sh when you want better compatibility across different Unix systems.

Basic Shell Scripting Concepts 📚

  • Variables : Variables store data that can be used and manipulated throughout your script.

Here’s an example on how to use them, Shell Script: Print a Statement
✅ Task: Print "I will complete #90DaysOfDevOps challenge"

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

Enter fullscreen mode Exit fullscreen mode

Shell Script: User Input + Arguments
✅ Task: Take input from user and from script argument

#!/bin/bash

# User input
read -p "Enter your name: " name

# Input from arguments
role=$1
tool=$2

echo "Hello, $name!"
echo "You are a $role working with $tool."
Enter fullscreen mode Exit fullscreen mode

Enter fullscreen mode Exit fullscreen mode

To run this script:
Save it as script.sh
Make it executable (Recommended)

`chmod +x script.sh`
Enter fullscreen mode Exit fullscreen mode

Then run it like this



./script.sh teacher computer


When Prompted
Enter your name: "Deborah"

Output will be

Hello, deborah!
You are a teacher working with computer.
Enter fullscreen mode Exit fullscreen mode

Shell Script: If-Else Example
✅ Task: Compare two numbers

#!/bin/bash

read -p "Enter first number: " a
read -p "Enter second number: " b

if [ "$a" -gt "$b" ]; then
  echo "$a is greater than $b"
elif [ "$a" -lt "$b" ]; then
  echo "$a is less than $b"
else
  echo "$a is equal to $b"
fi
Enter fullscreen mode Exit fullscreen mode

Top comments (0)