An argument that nobody gave is empty. Two forms say what to do then:
| You write | When v is empty |
|---|---|
${v:-text} | Use text instead |
${v:?message} | Write message as an error and stop the script: it fails |
text
learner@reef:~$ cat label.sh
#!/bin/bash
# Usage: label.sh ITEM [SIZE] ← [ ]: you can leave it out
item="${1:?give an item}"
size="${2:-small}"
echo "$size $item"
learner@reef:~$ ./label.sh scone large
large scone
learner@reef:~$ ./label.sh scone
small scone ← no $2: the default
learner@reef:~$ ./label.sh
./label.sh: line 3: 1: give an item ← script, line, the empty $1, your messageEach line of ~/logs/SHOP/register.log has a level: ERROR, WARN or INFO. Write ~/register-errors.sh SHOP [LEVEL], which counts the lines with that level. With no level, it counts ERROR:
text
learner@reef:~$ ./register-errors.sh north
north ERROR: 2
learner@reef:~$ ./register-errors.sh north WARN
north WARN: 1- Write it with
nano ~/register-errors.sh: the shop from$1, the level from$2with a default ofERROR. - Make it runnable with
chmod u+x ~/register-errors.sh, then run./register-errors.sh north. - Try
./register-errors.sh north WARN. - Make it refuse a missing shop with
${1:?…}, and try./register-errors.shwith nothing.
Objectives
- With no level, it counts the
ERRORlines. - It counts the level that you give.
- With no shop, it stops with a message as an error.