Shell Scripting: Novice to Pro Guide
1. Novice Level (Command Line Basics)
1.1 Introduction
- What is Shell? (Bash, Zsh, Sh).
- Navigation:
cd, ls, pwd.
- File Ops:
touch, mkdir, cp, mv, rm.
1.2 Permissions & Users
chmod (777 vs 755), chown, chgrp.
- Users:
sudo, su, /etc/passwd.
1.3 Basic Scripting
- Shebang (
#!/bin/bash).
- Running scripts:
./script.sh vs bash script.sh.
- Comments (
#).
- Variables:
NAME="Mahesh", accessing $NAME.
echo for printing.
read for user input.
- Redirection:
> (overwrite), >> (append), < (input).
- Pipes:
| (passing output to next command).
2.1 Control Flow
if, if-else, elif.
- Conditionals:
[-f file], [-d dir], [-z string], eq, ne, lt, gt.
- Loops:
for i in {1..5}
while [ condition ]
until
2.2 Arguments & Parameters
$1, $2... (Positional params).
$# (Count), $@ (All args).
$? (Exit status of last command).
2.3 Functions
- Definition:
func_name() { ... }.
- Passing arguments to functions.
- Local variables (
local var).
2.4 Text Processing Tools
grep: Searching patterns.
cut: Extracting columns.
head, tail, sort, uniq.
wc (word count).
3. Advanced Level (Automation mastery)
3.1 Advanced Text Processing
- AWK: Column based processing, math, formatted printing.
- SED: Stream editor, find/replace (
s/old/new/g), deleting lines.
- Regex basics.
3.2 Error Handling & Debugging
set -e (Exit on error).
set -x (Debug mode/trace).
- Traps:
trap 'cleanup' SIGINT (Handling Ctrl+C).
3.3 Arrays & Arithmetic
- Arrays:
arr=(a b c), ${arr[0]}.
- Arithmetic expansion:
$(( a + b )).
3.4 Cron Jobs
crontab -e.
- Scheduling formats (
* * * * *).
4. Expert Level (System Automation)
4.1 System Monitoring APIs
top, htop, ps, free.
- Checking disk usage (
du -sh, df -h).
- Parsing Log files automatically.
4.2 interacting with Web
curl and wget.
- Parsing JSON with
jq.
- Automating API calls.
4.3 Script Best Practices
- Portable scripts.
- Logging functions (writing to var/log).
- Parameter expansion
${var:-default}.
- Writing interactive menus (
select).