Chapter 5: Bash Scripting for Automation
Friends, today we learn Bash scripting. Stop typing the same two or ten commands every morning. One script — health check, pull, restart. Shebang, variables, quotes, exit codes, if / loops, a little idempotent habit, cron, and never hardcode secrets. Do not fear: keep the first script short. Not perfect poetry — clear and safe.
मित्रांनो, आज आपण Bash scripting शिकूया. दररोज सकाळी दोन-दहा commands टाइप करण्याची पद्धत सोडा, एक script — health check, pull, restart. Shebang, variables, quotes, exit codes, if / loops, थोडी idempotent habit, cron, आणि secrets hardcode नाही. घाबरू नका: पहिली script short ठेवा; perfect poetry नाही — clear आणि safe.
दोस्तों, आज हम Bash scripting सीखेंगे. हर सुबह दो-दस commands टाइप करने की आदत छोड़ो, एक script — health check, pull, restart. Shebang, variables, quotes, exit codes, if / loops, थोड़ी idempotent आदत, cron, और secrets hardcode नहीं. घबराओ मत: पहली script छोटी रखो; perfect poetry नहीं — clear और safe.
What you will learn in this chapter
- Shebang, variables, quotes, exit codes
- if and loops for health checks
- Check-before-install script habits
- Cron basics and PATH pitfalls
- Keep secrets out of scripts
Concepts in this chapter
- 5.1Why script — the morning ten commands
- 5.2Shebang, permissions, first script
- 5.3Variables, quotes and exit codes
- 5.4if and loops for health checks
- 5.5Idempotent-ish habits — check before install
- 5.6Cron basics — schedule the script
- 5.7Secrets — never hardcode
- 5.8Reading arguments and a full sample script
- 5.9Functions — keep scripts readable
- 5.10Debugging scripts without guessing
- 5.11Lab — health check script with dry-run
The chapter recap is at the end of the last concept page.
Practice Tasks
Suppose we are on the DevOps team at Nykaa, the beauty retail company, working from Mumbai. Pooja Mehta leads the team, and we look after the product review service. Costs are counted in INR. In the tasks below we do real DevOps jobs on this project using what this lesson teaches. Nykaa is a real company, but this project, the people, servers, IP addresses and numbers are made up for practice.
0 / 12
Easy Loop over servers
Why: Loops repeat a check on every server without copy-paste.
What to do: Write a bash script that loops over web1 web2 web3 and for each runs ssh "$h" uptime.
- Start the script with #!/bin/bash.
- Write one step per line: variables, tests, loops.
- Close every if with fi and every loop with done.
- Click Check answer.
Easy Find errors in a log
Why: grep shows only the lines that contain a word, so you find problems fast.
What to do: Show every line containing ERROR in /var/log/nykaa/app.log.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Click Check answer.
Medium Disk usage alert
Why: A full disk stops databases and builds without warning.
What to do: Write a bash script: USAGE=$(df / --output=pcent | tail -1 | tr -dc '0-9'); if [ "$USAGE" -gt 80 ], echo "Disk at $USAGE%" and exit 2; close the if.
- Start the script with #!/bin/bash.
- Write one step per line: variables, tests, loops.
- Close every if with fi and every loop with done.
- Click Check answer.
Medium Health-check script
Why: A tiny script in cron alerts you before customers notice.
What to do: Write a bash script: set URL="https://nykaa.example.com/health"; if curl -fs "$URL" > /dev/null succeeds, echo "UP", else echo "DOWN" and exit 1.
- Start the script with #!/bin/bash.
- Write one step per line: variables, tests, loops.
- Close every if with fi and every loop with done.
- Click Check answer.
Medium Safe deploy script header
Why: set -euo pipefail stops a script at the first error instead of carrying on.
What to do: Write a bash script that starts with set -euo pipefail, sets TAG="$1", then runs docker pull nykaa/product-review:"$TAG".
- Start the script with #!/bin/bash.
- Write one step per line: variables, tests, loops.
- Close every if with fi and every loop with done.
- Click Check answer.
Medium Count matching lines
Why: A count tells you quickly how often something happened today.
What to do: Count how many lines in /var/log/nginx/access.log contain " 404 ".
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Click Check answer.
Medium Case-insensitive search with line numbers
Why: Logs mix "Timeout" and "TIMEOUT". Line numbers help you open the exact spot.
What to do: Search for timeout in /var/log/nykaa/api.log, ignoring upper/lower case, and show line numbers.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Click Check answer.
Medium Find files by name
Why: find searches folder trees for files that match a pattern.
What to do: Find all files ending in .log under /var/log/nykaa.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Click Check answer.
Hard Dated backup with cleanup
Why: Backups need a date in the name and old ones removed, or disks fill up.
What to do: Write a bash script: D=$(date +%F); tar -czf /backup/nykaa-$D.tar.gz /var/www; then find /backup -name "*.tar.gz" -mtime +7 -delete.
- Start the script with #!/bin/bash.
- Write one step per line: variables, tests, loops.
- Close every if with fi and every loop with done.
- Click Check answer.
Hard Search a whole folder
Why: Settings can hide in any file. A recursive search checks them all.
What to do: Find every line that mentions DB_HOST in all files under /etc/nykaa, with line numbers.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Click Check answer.
Hard Find old files
Why: Old files fill the disk. -mtime +N finds files changed more than N days ago.
What to do: Find regular files under /backup/nykaa that were changed more than 35 days ago.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Click Check answer.
Hard Find big files
Why: When the disk is full, find large files first.
What to do: Find files bigger than 100 MB anywhere under /var.
- Find the Linux command that does this job.
- Add the options (like -r or -h) the task needs.
- Add the file names, folders or users in the right order.
- Click Check answer.
Ravi Sir's Cards: 25 revision, interview and comparison cards for this chapter · मराठी cards