Shell Scripting for DevOps
By @LENKA KARTHEEK
🛠 Core Shell Scripting Skills
✔ Bash script structure ( ) #!/bin/bash
✔ Variables, user input, and arguments
✔ Conditional logic ( ) if-else
✔ Loops ( , ) for while
✔ Functions and reusable logic
✔ Exit codes and error handling ( , set -e $? )
✔ Debugging ( ) set -x
⚙ Linux Automation Use-Cases
✔ Service health checks & auto-restart
✔ Log cleanup using & find cron
✔ Disk, memory, and system monitoring scripts
✔ Permission and process management
✔ Environment-based scripting (dev / stage / prod)
🚀 DevOps-Focused Scripts
✔ CI/CD helper scripts (build, deploy, restart services)
✔ Git automation inside pipelines
✔ Docker and container-based scripting
✔ Cron job scheduling for routine tasks
✔ Safe, readable, production-friendly scripts
1. Basic Shell Commands (DevOps Daily Use)
Shell Scripting for DevOps 1
File & Directory
ls -l# list files
pwd# current directory
cd /var/log# change directory
mkdir app# create directory
rm -rf temp# delete directory forcefully
cp file1 file2# copy file
mv old new# rename/move file
Permissions
chmod 755 [Link]# executable permission
chown user:group file
Process & System
ps aux# running processes
top# live system usage
df -h# disk usage
free -m# memory usage
uptime# system load
2. Basic Shell Script Structure
#!/bin/bash
echo"Hello DevOps!"
Make executable:
chmod +x [Link]
./[Link]
Shell Scripting for DevOps 2
3. Variables & User Input
#!/bin/bash
NAME="Kartheek"
echo"Welcome $NAME"
read -p"Enter environment: " ENV
echo"Deploying to $ENV"
4. If-Else Conditions (Very Common)
Check if file exists
#!/bin/bash
FILE="/etc/passwd"
if [ -f"$FILE" ];then
echo"File exists"
else
echo"File not found"
fi
Check command success
if systemctl is-active nginx >/dev/null;then
echo"Nginx is running"
else
echo"Nginx is stopped"
fi
Shell Scripting for DevOps 3
5. Loops (Automation Core)
For loop
for serverin server1 server2 server3
do
echo"Deploying to $server"
done
While loop
count=1
while [$count -le 5 ]
do
echo"Attempt $count"
((count++))
done
6. Functions (Reusable Logic)
#!/bin/bash
deploy() {
echo"Deploying application..."
}
deploy
7. Example: Service Health Check Script
#!/bin/bash
Shell Scripting for DevOps 4
SERVICE="nginx"
if systemctl is-active --quiet$SERVICE;then
echo"$SERVICE is running"
else
echo"$SERVICE is down, restarting..."
systemctl restart$SERVICE
fi
8. Example: Log Cleanup Script (Very Common in
DevOps)
#!/bin/bash
LOG_DIR="/var/log/myapp"
DAYS=7
find$LOG_DIR -type f -mtime +$DAYS -execrm -f {} \;
echo"Old logs deleted"
9. Example: Disk Usage Alert Script
#!/bin/bash
THRESHOLD=80
USAGE=$(df / | awk'NR==2 {print $5}' | sed's/%//')
if ["$USAGE" -gt"$THRESHOLD" ];then
echo"Disk usage is above $THRESHOLD%"
else
Shell Scripting for DevOps 5
echo"Disk usage is normal"
fi
10. Example: CI/CD Deployment Script
#!/bin/bash
set -e# stop on error
echo"Pulling latest code"
git pull origin main
echo"Building application"
npm install
npm run build
echo"Restarting service"
pm2 restart app
echo"Deployment successful"
11. Error Handling & Debugging
set -e# exit on error
set -x# debug mode (prints commands)
12. Cron Job Example (Automation Scheduling)
crontab -e
Run script every day at midnight:
Shell Scripting for DevOps 6
0 0 * * * /home/user/[Link]
13. DevOps Best Practices for Shell Scripts
✔ Always use #!/bin/bash
✔ Use for safety
set -e
✔ Log output to files
✔ Use functions
✔ Avoid hardcoding secrets
✔ Add comments
Shell Scripting for DevOps 7