Shell Scripting Basics
Automate everything. Why type commands 100 times when a script does it once?
Why Shell Scripts?
Shell scripts are the automation backbone of Linux. Security professionals use them to automate reconnaissance, process logs, deploy tools, and create custom utilities. A single script can replace hours of manual work.
Your First Script
#!/bin/bash
# My first security script
echo "=== System Recon ==="
echo "Hostname:" $(hostname)
echo "Current user:" $(whoami)
echo "IP Address:" $(hostname -I)
echo "Kernel:" $(uname -r)
Save as recon.sh, make executable with chmod +x recon.sh, then run with ./recon.sh
Essential Concepts
Shebang (#!/bin/bash)
First line tells the system which interpreter to use. Always include it.
Variables
NAME="value" to set, $NAME to use. No spaces around =
Command Substitution
$(command) captures output. Example: DATE=$(date)
Arguments
$1, $2 are arguments. $# = count, $@ = all
Loops and Conditionals
# Port scanner example
for port in 22 80 443 8080; do
if nc -z -w1 $1 $port 2>/dev/null; then
echo "[OPEN] Port $port"
fi
done
Run with: ./scanner.sh 192.168.1.1
LAB: Script Workshop
Build a system reconnaissance script! Write code in the editor and click Run to execute it. Complete all objectives to master scripting basics.
#!/bin/bash, create a variable like USER=$(whoami), then use echo to display information.