Shell scripts · Linux and the Command Line
How do you turn commands you repeat into a script?
Course contentsWhy you need it
If you type the same five commands every morning, a script types them for you, the same way every time. Deploy scripts, backups and health checks on real servers are mostly small shell scripts built from what you already know, plus variables, conditions and loops.
The intuition
- Variable:
name=ada(no spaces around=), used as$name. - Command result:
n=$(grep -c ERROR logs/app.log)stores what the command prints. - Maths:
$((n + 1)). - Condition:
if [ $n -gt 2 ]; then … else … fi. Compare numbers with-eq -ne -gt -lt -ge -le, test files with-f(file exists) and-d(folder exists). - Loop:
for f in logs/*.log; do … done.
Put "$var" in double quotes when it might contain spaces.
Definition
Command substitution
`$(command)` runs the command and puts what it printed in its place, so you can store it in a variable.
Exit status
Every command reports 0 for success and another number for failure. `if` runs the then-part when the test reports 0.
Examples
A health check
Run it in the lessonCount errors, then decide what to say.
errors=$(grep -c ERROR logs/app.log)
echo "errors today: $errors"
if [ $errors -gt 2 ]; then
echo "ALERT: too many errors"
else
echo "all fine"
fi- L1grep prints 3, which goes into the variable errors.
- L2$errors is replaced by 3 inside the double quotes.
errors today: 3 - L33 -gt 2 is true, so the then-part runs.
ALERT: too many errors
Loop over files
Run it in the lessonFor each text file in notes, print its name and line count. A counter adds them up.
total=0
for f in notes/*.txt; do
n=$(wc -l < $f)
echo "$f: $n"
total=$((total + n))
done
echo "total: $total"- L2notes/*.txt expands to ideas.txt and todo.txt, in alphabetical order.
- L3
wc -l < fileprints only the number, without the file name. - L5Add to the running total.
- L73 + 3.
total: 6
Common mistakes
Spaces around =
n = 3 runs a command called n. Write n=3.
Missing spaces inside [ ]
[$n -gt 2] fails; the brackets need spaces: [ $n -gt 2 ].
Quick quiz
1. What does this print?
n=0
for w in red green blue; do
n=$((n + 1))
echo "$n $w"
doneShow the answer
Answer: A
n goes up by one before each echo.
2. What does this print?
if [ -f logs/app.log ]; then echo found; else echo missing; fi
if [ -d logs/app.log ]; then echo folder; else echo "not a folder"; fiShow the answer
Answer: A
app.log exists and is a file, not a folder.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictFollow the script
Type exactly what this prints.
- ModifyMake it a warning
The script always prints the count. Change it so it prints
WARN: 3 errorswhen there are more than 2 errors andOK: N errorsotherwise. - WriteBack up every note
Make a folder
backup. Then, with aforloop overnotes/*.txt, copy each file intobackup/and printsaved FILEfor each one (FILE is the loop variable, e.g.saved notes/ideas.txt).
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Shell script | shell 脚本 | A file of shell commands run one after another. |
| Shell variable | shell 变量 | A name set with name=value and read with $name. |
| Exit status | 退出状态 | The number a command ends with: 0 means success. |