DevOps Shell Scripting
A shell script is a plain text file that holds a list of commands. The shell reads the file from top to bottom and runs each command in order. DevOps engineers write shell scripts to automate repeated jobs such as backups, log cleanup, server setup, and health checks.
Why Shell Scripts Matter
Picture a recipe card in a kitchen. The card lists every step, so the cook never skips an ingredient. A shell script works like that card for a computer. You write the steps once, and the computer follows them the same way every time.
- Scripts save time on repeated tasks.
- Scripts reduce human mistakes.
- Scripts run on almost every Linux server without extra software.
- Scripts connect other DevOps tools inside pipelines.
How a Script Runs
script.sh ---> Shell (bash) ---> Operating System ---> Result (recipe) (reads lines) (does the work) (output)
The shell acts as a translator between your script and the operating system. Bash is the most popular shell on Linux servers.
Your First Script
Create a file named hello.sh with these two lines.
#!/bin/bash
echo "Hello, DevOps"The first line, called the shebang, tells the system which shell should run the file. The second line prints a message on the screen. Make the file runnable and start it with these commands.
chmod +x hello.sh
./hello.shCore Building Blocks
Variables
A variable stores a value under a name. Write the name, an equals sign, and the value with no spaces around the equals sign. Read the value later by placing a dollar sign before the name.
name="Asha"
echo "Welcome, $name"Conditions
A condition lets the script choose between paths. The if keyword checks a test and runs the commands inside only when the test passes.
if [ -f /etc/passwd ]; then
echo "File exists"
else
echo "File missing"
fiLoops
A loop repeats commands for every item in a list. A loop saves you from typing the same command ten times.
for server in web1 web2 web3; do
ping -c 1 $server
doneFunctions
A function groups commands under one name. Call the name whenever you need those commands again.
say_hi() {
echo "Hi, $1"
}
say_hi "Ravi"Exit Codes
Every command ends with a number. The number 0 means success, and any other number means failure. The special variable $? holds the exit code of the last command. Pipelines read these codes to decide whether to continue or stop.
Practical Example: Disk Space Checker
This script warns the team when the main disk fills above 80 percent.
#!/bin/bash
limit=80
usage=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$usage" -gt "$limit" ]; then
echo "Warning: disk usage is ${usage}%"
exit 1
else
echo "Disk usage is healthy: ${usage}%"
fiThe df command reports disk usage. The awk tool picks the percentage column, and tr removes the percent sign. The if block compares the number with the limit. A cron job can run this script every hour and alert the team on failure.
Command-Line Arguments and User Input
Scripts become reusable when they accept input. The variables $1, $2, and so on hold the first, second, and later values typed after the script name. The variable $# counts the values, and $@ lists them all. The read command asks the user to type a value while the script runs.
#!/bin/bash
echo "Backing up folder: $1"
read -p "Continue? (y/n) " answer
if [ "$answer" = "y" ]; then
tar -czf backup.tar.gz "$1"
fiRun the script as ./backup.sh /var/www. The folder name becomes $1.
Pipes and Redirection
A pipe (|) sends the output of one command into the input of the next command. Redirection (> and >>) sends output into a file instead of the screen. The symbol 2> redirects error messages.
cat app.log --> grep ERROR --> sort --> uniq -c --> report.txt (read) (filter) (order) (count) (save)
cat app.log | grep ERROR | sort | uniq -c > report.txtThe tools grep, sed, and awk handle searching, replacing, and column work. Small tools linked by pipes solve large problems.
Scheduling Scripts with Cron
Cron runs commands at fixed times. Edit the schedule with crontab -e. Each line holds five time fields followed by the command.
* * * * * command | | | | | | | | | +-- day of week (0-6) | | | +---- month (1-12) | | +------ day of month (1-31) | +-------- hour (0-23) +---------- minute (0-59)
30 2 * * * /opt/scripts/backup.shThe line above runs the backup script at 2:30 every night.
Safe and Debuggable Scripts
Start serious scripts with set -euo pipefail. The -e option stops the script at the first failed command. The -u option stops on unset variables, and pipefail reports a failure anywhere inside a pipe. Run a script with bash -x script.sh to print each command before it runs. The trace shows exactly where a problem begins.
The trap command runs cleanup steps, such as deleting temporary files, when a script exits or fails.
trap 'rm -f /tmp/work.tmp' EXITKey Points
- A shell script is a saved list of commands.
- Variables, conditions, loops, and functions form the core of every script.
- Exit codes tell pipelines whether a step worked.
- Small, tested scripts remove repeated manual work.
