Linux on call · Chapter 25 of 30
Conditions and exit codes
By the end of this chapter you can make a script check its input, refuse what is wrong with a clear message and an exit code, and act on another command's success or failure. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Shell scripting
- 5 tasks
- Linux
- In the all labs plan
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.
Why you need this
The counter screen loaded an empty menu this morning and showed "Today: " to every customer. Bunty's check-menu.sh was meant to stop that. Kabir ran it on a file that does not exist:
cat check-menu.sh
./check-menu.sh nothing.txt
echo $?
#!/bin/bash
# Check a menu file before the counter screen loads it.
cat $1 > /dev/null
echo "OK: $1"
cat: nothing.txt: No such file or directory
OK: nothing.txt
0
The screen's loader never reads the words: it reads that last 0, which means "all good". A script that runs every line from top to bottom cannot stop at a problem. Kabir's note, NOTE.txt, lists what each case must print and return.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
A good menu: OK: FILE has N lines, exit code 0
Make check-menu.sh say OK for a good menu, with the number of lines in it.
1. Open check-menu.sh. Bunty's script says OK for every file.
2. Make its last line print OK: FILE has N lines. FILE is the name as it was given, and N is the number of lines in the file.
3. Count the lines with wc -l and <, so only the number comes out. Put that number into the line with $( ).
4. Run ./check-menu.sh menu.txt, then echo $? to see the exit code.
To pass: for a file that exists and is not empty, ./check-menu.sh FILE prints exactly one line, like OK: good.txt has 3 lines for a 3-line good.txt, and nothing else on either stream, and it exits with code 0.
- Task 2
Refuse a missing or empty file with exit code 1
Make check-menu.sh refuse a file that is missing or empty.
1. Before the OK line, check if the file exists. Use if and [ ! -f "$1" ]. If it does not exist, print Missing: FILE and exit 1.
2. Then check if the file is empty. Use [ ! -s "$1" ]. If it is empty, print Empty: FILE and exit 1.
3. Delete Bunty's line with cat. Its error message would show too.
4. Run ./check-menu.sh nothing.txt and ./check-menu.sh specials.txt. After each, run echo $?.
To pass: for a missing file, the script prints only Missing: FILE and exits with code 1. For an empty file, it prints only Empty: FILE and exits with code 1. No other message appears, on either stream.
- Task 3
No file given: the usage line and exit code 2
Make check-menu.sh explain how to use it when you give it no file.
1. Make this the first check in the script, before the other checks. Use if and [ -z "$1" ].
2. If there is no argument, print Usage: check-menu.sh FILE and exit 2.
3. Run ./check-menu.sh with nothing after it, then run echo $?.
To pass: ./check-menu.sh with no argument prints only Usage: check-menu.sh FILE and exits with code 2.
- Task 4
Save the exit code for specials.txt in exit-code.txt
Save the exit code your script gives for specials.txt.
1. Finish the script first (tasks 1 to 3).
2. Run ./check-menu.sh specials.txt in the chapter folder.
3. As the very next command, save $? in exit-code.txt. echo with > writes it into the file.
To pass: exit-code.txt has the exit code of ./check-menu.sh specials.txt, only the number, and you read it from $? in the chapter folder. Any command in between changes $?, so save it right away.
- Task 5
Write menu ok or menu broken into status.txt, in one line
Act on the script's exit code in one command line.
1. Start the line with ./check-menu.sh menu.txt.
2. Then add && and a command that writes menu ok into status.txt. && runs it only if the check works.
3. Then add || and a command that writes menu broken into status.txt. || runs it only if the check fails.
4. Run cat status.txt to see what was written.
To pass: you typed this one line (with ./check-menu.sh, then &&, then ||, in that order) in the chapter folder, and status.txt has menu ok and nothing else.
What you will learn
- Exit codes and $?: the number every command ends with.
- test, or [ ]: checks on files and strings that answer with an exit code.
- if and exit: run some lines only when a check passes, and stop the script with the code you choose.
- && and ||: run the next command only after a success, or only after a failure.
More in Shell scripting
Build "Conditions and exit codes" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.