Chapter 4: Linux Advanced Commands
4.13 Basic shell scripting
In short: A shell script is a text file containing commands, run top to bottom.
It's very simple – a script is just commands you already know, written in one file. Come on, let's write a script together: a small health report for the Mauli Dairy server.
एकदम सिंपल आहे – स्क्रिप्ट म्हणजे तुम्हाला आधीच येणाऱ्या कमांड्स, एकाच फाइलमध्ये लिहिलेल्या. चला, एकत्र एक स्क्रिप्ट लिहूया: Mauli Dairy च्या सर्व्हरसाठी एक छोटा हेल्थ रिपोर्ट.
बिल्कुल सिंपल है – स्क्रिप्ट मतलब वो कमांड्स जो आपको पहले से आती हैं, एक ही फ़ाइल में लिखी हुईं. चलो, साथ में एक स्क्रिप्ट लिखते हैं: Mauli Dairy के सर्वर के लिए एक छोटी हेल्थ रिपोर्ट.
A shell script is a text file containing commands, run top to bottom. It is the first step towards automation (and towards EC2 user data, which runs a script at first boot).
#!/bin/bash
# file: sysinfo.sh - print a quick health report
set -uo pipefail # stop on undefined variables and pipe failures
NAME=$(hostname)
echo "Health report for $NAME at $(date)"
echo "---------------------------------"
echo "Uptime : $(uptime -p)"
echo "Disk / : $(df -h / | awk 'NR==2 {print $5 " used"}')"
echo "Memory : $(free -m | awk '/Mem/ {printf "%d/%d MB used", $3, $2}')"
# if-else
if systemctl is-active --quiet nginx; then
echo "Nginx : running"
else
echo "Nginx : NOT running"
fi
# loop
for svc in sshd crond; do
echo "$svc -> $(systemctl is-active $svc 2>/dev/null)"
done
# function with argument
check_port() {
if ss -tln | grep -q ":$1 "; then echo "Port $1 open"; else echo "Port $1 closed"; fi
}
check_port 22
check_port 80
nano sysinfo.sh # paste the script, save with Ctrl+O, exit with Ctrl+X
chmod +x sysinfo.sh
./sysinfo.sh
| Concept | Syntax |
|---|---|
| Variable | NAME="Asha", use $NAME or ${NAME} (no spaces around =) |
| Command output | TODAY=$(date +%F) |
| Arguments | $1, $2, all: $@, count: $# |
| Exit status of last command | $? (0 = success) |
| Test file exists | if [ -f /etc/nginx/nginx.conf ]; then ... fi |
| Test directory | [ -d /var/www ] |
| Compare numbers | [ "$a" -gt 10 ] (-eq -ne -lt -le -ge) |
| Compare strings | [ "$a" = "yes" ] |
| Read input | read -p "Enter name: " NAME |
Ravindra Bagale's Tip
Many students write a script in Notepad on Windows, copy it to the server and get an error like bad interpreter – because of Windows line endings (CRLF). Fix it with sed -i 's/\r$//' script.sh, or write the script on the server itself in nano. And don't forget chmod +x.
Ravindra Bagale's Tip – मराठी
बरेच students Windows वर Notepad मध्ये script लिहून server वर copy करतात आणि bad interpreter सारखा error येतो – कारण Windows line endings (CRLF). sed -i 's/\r$//' script.sh ने fix करा, किंवा script server वरच nano मध्ये लिहा. आणि chmod +x विसरू नका.
Ravindra Bagale's Tip – हिंदी
बहुत से students Windows पर Notepad में script लिखकर server पर copy करते हैं और bad interpreter जैसा error आता है – क्योंकि Windows line endings (CRLF). sed -i 's/\r$//' script.sh से fix करो, या script server पर ही nano में लिखो. और chmod +x मत भूलना.
Lab
Chala, sysinfo.sh copy karun chalva, mag tyat check_port 443 aani check_port 3306 jodun parat chalva. Nantar ek navin script backup.sh liha jo ~/backups/web-$(date +%F).tar.gz banvel, aani ./backup.sh chalvun file banli ka te ls ~/backups ne check kara.