Skip to main content

Stage 4: Script Champion

Teach learners how to write and execute shell scripts to automate repetitive tasks. Build confidence in scripting fundamentals like variables, loops, conditionals, and execution permissions - skills often tested in interviews.


Hackbook Overview

1. What is a Shell Script?

  • A text file containing a sequence of commands.
  • Automates tasks that would otherwise be run manually.
  • Commonly written in Bash.

2. Creating and Running Scripts

  • Create a file: nano script.sh.
  • Make executable: chmod +x script.sh.
  • Run: ./script.sh.

Add commands:

#!/bin/bash
echo "Hello, Linux!"

3. Variables

  • Store values for reuse.

Example:

NAME="Shubham"
echo "Hello $NAME"

4. Conditionals

  • Use if, else, elif.

Example:

if [ -f file.txt ]; then
  echo "File exists"
else
  echo "File not found"
fi

5. Loops

While loop:

while true; do
  echo "Running..."
  break
done

For loop:

for i in 1 2 3; do
  echo "Number $i"
done

6. Why This Matters in Interviews

  • Shows ability to automate tasks and think like an engineer.
  • Demonstrates practical knowledge beyond just running commands.
  • Often tested in DevOps, Cloud, and SysAdmin interviews.

Hands‑On Practice

  • Write a script that prints today’s date.
  • Write a script that checks if a file exists.
  • Write a script that loops through numbers 1–5 and prints them.
  • Write a script that backs up a directory into a .tar.gz file.

Interview Question Bank

Conceptual

  • Q1. What is a shell script and why is it useful?
    A1. A shell script is a file containing commands executed by the shell. It automates repetitive tasks, saving time and reducing errors.
  • Q2. How do you make a script executable?
    A2. Use chmod +x script.sh and run it with ./script.sh.
  • Q3. What is the purpose of #!/bin/bash at the top of a script?
    A3. It’s called a shebang. It tells the system which interpreter to use (here, Bash).

Practical

Q6. How do you check if a file exists in a script?
A6.bash

if [ -f filename ]; then
  echo "File exists"
fi

Q5. How do you pass arguments to a script?
A5. Arguments are accessed as $1, $2, etc. Example:bash

echo "First argument: $1"

Q4. Write a script that prints “Hello World.”
A4.bash

#!/bin/bash
echo "Hello World"

Scenario‑Based

  • Q7. You need to automate log cleanup every day. How would you do it?
    A7. Write a script that deletes old logs and schedule it with cron.

Q9. How would you write a script that checks disk usage and alerts if it’s above 80%?
A9.bash

#!/bin/bash
usage=$(df / | grep / | awk '{print $5}' | sed 's/%//')
if [ $usage -gt 80 ]; then
  echo "Disk usage above 80%!"
fi

Q8. You want to back up /home/user into /backup. How do you script it?
A8.bash

#!/bin/bash
tar -czf /backup/home_backup.tar.gz /home/user

Behavioral Based

  • Q10. Tell me about a time you automated a task with a script.
    A10. Example: “I wrote a script to monitor log files and alert when errors appeared, which reduced downtime during deployments.”

Cheatsheet (Quick Notes)

  • Create Script: nano script.sh → add commands → chmod +x script.sh./script.sh.
  • Shebang: #!/bin/bash defines interpreter.
  • Variables: VAR=value, access with $VAR.
  • Conditionals: if [ condition ]; then … fi.
  • Loops: for, while.
  • Arguments: $1, $2, etc.

Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 21, 2025