Ravindra BagaleCourses & study guides मराठी

DevOps

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.

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

  1. 5.1Why script — the morning ten commands
  2. 5.2Shebang, permissions, first script
  3. 5.3Variables, quotes and exit codes
  4. 5.4if and loops for health checks
  5. 5.5Idempotent-ish habits — check before install
  6. 5.6Cron basics — schedule the script
  7. 5.7Secrets — never hardcode
  8. 5.8Reading arguments and a full sample script
  9. 5.9Functions — keep scripts readable
  10. 5.10Debugging scripts without guessing
  11. 5.11Lab — health check script with dry-run

The chapter recap is at the end of the last concept page.

Practice Tasks

About this scenario

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.

How:
  1. Start the script with #!/bin/bash.
  2. Write one step per line: variables, tests, loops.
  3. Close every if with fi and every loop with done.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Start the script with #!/bin/bash.
  2. Write one step per line: variables, tests, loops.
  3. Close every if with fi and every loop with done.
  4. 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.

How:
  1. Start the script with #!/bin/bash.
  2. Write one step per line: variables, tests, loops.
  3. Close every if with fi and every loop with done.
  4. 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".

How:
  1. Start the script with #!/bin/bash.
  2. Write one step per line: variables, tests, loops.
  3. Close every if with fi and every loop with done.
  4. 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 ".

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Start the script with #!/bin/bash.
  2. Write one step per line: variables, tests, loops.
  3. Close every if with fi and every loop with done.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. 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.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Click Check answer.

Ravi Sir's Cards: 25 revision, interview and comparison cards for this chapter · मराठी cards