Skip to main content

Short note: shell scripts

Short answer

A shell script is a text file of commands executed by a shell to automate tasks. It uses variables, conditionals, loops, and functions. Common uses include backups, log cleanup, and cron jobs. Simple and portable but slower than compiled programs.

Write a short note on shell scripts.

A shell script is a text file containing a sequence of shell commands and control statements, which the shell (sh, bash, ksh) interprets and executes line by line. It is used to automate repetitive tasks, especially in system administration.

Features

  • Variables, input (read), arithmetic.
  • Control structures: if, case, for, while.
  • Functions, command-line arguments ($1, $2, $#), exit status $?.
  • Pipes, redirection and wildcards.
  • Interpreted: no compilation.

Example

#!/bin/bash
# backup home directory and delete old backups
DATE=$(date +%F)
tar -czf /backup/home-$DATE.tar.gz /home
if [ $? -eq 0 ]; then
    echo "Backup done on $DATE"
fi
find /backup -mtime +7 -delete

Make it executable with chmod +x backup.sh and run ./backup.sh.

Uses

  • Automating backup, log cleaning, user creation.
  • Running at set times with cron.
  • Start-up and shutdown scripts at boot.
  • Batch processing of files.

Advantages: simple, quick to write, portable. Disadvantages: slower than compiled programs, and poor for complex programs.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Operating System chapter 9 questions →