task 7.1 · chapter 7 of 8: a little bash scripting · 25 min
Is everyone up?
objective
Finish scripts/reach.sh: a script that reads a list of hosts, pings each one, prints up or down, and exits 1 when any of them is down, so other scripts and pipelines can tell.
scripts/reach.shopen a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter
| Lesson | A little Bash scripting |
|---|---|
| The list | hosts.txt: ws1, srv1, r1, and one that was switched off |
| Your script | scripts/reach.sh: two TODOs |
new to this? step by stepbeginner
chmod +x scripts/reach.sh, then./scripts/reach.sh: it prints TODO lines.- TODO 7.1a: an
if ping -c 1 -W 1 "$host" >/dev/null 2>&1; then ... else ... fithat prints$host upor$host downand setsdown=1. - TODO 7.1b:
exit "$down"at the end. ./scripts/reach.sh; echo $?prints the four hosts, then 1. Thenmake check T=7.1.
more in meet the lab
A script is commands you’d type
What makes a file a script
A script is a text file of commands, run one after another, as if you’d typed them. Two things make it runnable by name:
- its first line, the shebang, says which program reads it:
#!/usr/bin/env bash; - it has
xpermission (chmod +x, chapter 4).
./scripts/reach.sh runs it; the ./ says the one here, because the lab’s folder isn’t on PATH.
Exit codes
Every command ends with an exit code, a number: 0 means success, anything else means something went
wrong. The shell keeps the last one in $?. if runs a command and looks at its exit code, so if ping ...
means if the ping worked. A script’s own exit code is what its exit says, which is how a script tells the
script that called it, or a CI pipeline, whether it worked.
in one sentence 0 is success; if tests a command’s exit code, and exit sets your script’s own.
Reading a file line by line
while read -r host; do
echo "got $host"
done < hosts.txt
read takes one line from the file into the variable host, the loop body runs, and it repeats until the file
runs out. < hosts.txt feeds the file into the whole loop. -r keeps backslashes as they are.
Quote your variables
"$host", not $host. Without quotes, the shell splits a value with spaces in it into separate words, and a
* in it turns into file names. With quotes, it stays one thing. Quote every $ unless you have a reason not to.
The starter
file="${1:-hosts.txt}" # the first argument, or hosts.txt if there isn't one
down=0 # becomes 1 as soon as one host doesn't answer
while read -r host; do
case "$host" in ""|"#"*) continue ;; esac # skip blank lines and comments
# TODO 7.1a: ping "$host" once (-c 1), waiting at most one second (-W 1), and throw its output away.
# If it answers, print "$host up". If it doesn't, print "$host down" and set down=1.
echo "TODO 7.1a is still open: $host"
done < "$file"
# TODO 7.1b: finish with exit code $down, so that 0 means every host answered.
$1 is the script’s first argument, and ${1:-hosts.txt} means $1, or hosts.txt if there isn’t one.
case skips blank lines and lines starting with #, and continue jumps straight to the next line.
Finish reach.sh
Make it runnable, and run the starter:
run in the terminalchmod +x scripts/reach.sh ./scripts/reach.shIt reads hosts.txt and prints a TODO line for each host. The loop works; it just doesn’t ping yet.
See what ping’s exit code looks like for a host that answers and one that doesn’t:
run in the terminalping -c 1 -W 1 ws1 >/dev/null 2>&1; echo $? ping -c 1 -W 1 172.80.102.99 >/dev/null 2>&1; echo $?0, then1.>/dev/null 2>&1throws away both ping’s output and its errors: you only want its exit code.Open scripts/reach.sh and replace the
echo "TODO 7.1a ..."line with:editscripts/reach.shCtrl+P → reach.shif ping -c 1 -W 1 "$host" >/dev/null 2>&1; then echo "$host up" else echo "$host down" down=1 fiAt the very end, under TODO 7.1b:
editscripts/reach.shCtrl+P → reach.shexit "$down"Run it, then ask for its exit code:
run in the terminal./scripts/reach.sh echo $?ws1 up srv1 up r1 up 172.80.102.99 down 1make check T=7.1runs it on hosts.txt and on two lists it hasn’t seen.
in one sentence if command; then ...; else ...; fi branches on an exit code; exit passes one on.
Use it the way scripts get used
The script is done. Now use its exit code, the way a CI job or another script would:
- Make a list of only the hosts that answer, in a file of your own, and run
./scripts/reach.shon it. What’s$?now? - Run it so that it says
all uponly when every host answered, with&&. Try it on both lists.
Nothing more to hand in: make check T=7.1 is the check.
hint
The script’s first argument is the file: ./scripts/reach.sh mylist.txt.
hint
a && b runs b only when a exited 0. a || b runs b only when a didn’t.
show the solution
printf 'ws1\nsrv1\n' > /tmp/up.txt
./scripts/reach.sh /tmp/up.txt && echo "all up"
./scripts/reach.sh || echo "something is down"
ws1 up
srv1 up
all up
That one exit code is what makes a script composable: a pipeline stage, a cron job or a pre-change check can all
stop on 1 without reading a word of the output.
5 check: run it, then prove it
$ make check T=7.1when it's right, you'll see
╭─ task 7.1 · is everyone up?
│ ✓ reach.sh on hosts.txt (no argument): one 'name up|down' line per host, exit code 1 (follow along)
│ ✓ reach.sh on a list it hadn’t seen: one 'name up|down' line per host, exit code 0 (follow along)
│ ✓ reach.sh on a list it hadn’t seen: one 'name up|down' line per host, exit code 1 (follow along)
╰─ 3/3 checks passed verified