Linux on call · Chapter 27 of 30
Safer scripts
By the end of this chapter you can write a script that stops at the first problem, keeps every name whole, and cleans up after itself, even when it fails. 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
Customers upload menus and photos, and some uploads arrive empty. Bunty wrote clean-uploads.sh to remove the empty ones. Kabir tried it on a copy of the folder, never the real one:
cp -r uploads /tmp/uploads-copy
./clean-uploads.sh /tmp/uploads-copy
rm: cannot remove 'chai': No such file or directory
Removed: chai
rm: cannot remove 'menu.pdf': No such file or directory
Removed: menu.pdf
Removed: draft.txt
rm: cannot remove 'empty': No such file or directory
Removed: empty
rm: cannot remove 'file.txt': No such file or directory
Removed: file.txt
rm: cannot remove 'new': No such file or directory
Removed: new
rm: cannot remove 'upload.png': No such file or directory
Removed: upload.png
rm: cannot remove 'order': No such file or directory
Removed: order
rm: cannot remove 'notes.txt': No such file or directory
Removed: notes.txt
Every name with a space fell apart, and only draft.txt really went. Then Kabir mistyped the folder. The script said cd: /tmp/uplaods-copy: No such file or directory, carried on in the folder he was standing in, and removed an empty todo.txt there. With no folder at all, cd goes to your home folder and cleans that. Both times it ended with exit code 0, "all good". Quoting alone (chapter 24) does not fix a script that keeps going after a failure.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Fix clean-uploads.sh: delete only the empty files
Fix Bunty's script so it deletes only the empty files, even names with spaces.
1. Open clean-uploads.sh and read it. Keep line one, the bash shebang. Keep it executable.
2. The script gets a folder as its first argument, like ./clean-uploads.sh DIR.
3. Delete every empty file in that folder, and keep every file that has something in it.
4. Do not move into the folder with cd. Use the folder path in the commands.
To pass: the Check runs the script from another folder, on a test folder. Only the empty files in the test folder are gone, names with spaces too. An empty file in the folder it was run from is still there. The script ends with exit code 0.
- Task 2
Strict mode: no folder or a wrong one deletes nothing
Make the script stop safely when the folder is missing or wrong.
1. Put the line set -euo pipefail right under the shebang, on its own line.
2. If no folder is given, the script must stop with an error.
3. If the folder does not exist, the script must stop with an error.
To pass: the script has the line set -euo pipefail (set -eu -o pipefail works too). Run with no folder, or with a folder that does not exist, it ends with an exit code that is not 0. It deletes nothing: not in the folder it was run from, not in your home folder, not in the uploads folder.
- Task 3
Do the work in a function with local variables
Put the work of the script inside a function.
1. Make a function, for example clean() { ... }.
2. Move the search and the delete loop inside it.
3. Inside it, keep its variables with local. Write the word local in front of each one, like local dir=$1.
4. Below the function, call it with the script's first argument: clean "$1".
To pass: the script has a function, a line that starts with local, and a line that calls the function. It still deletes exactly the empty files, as in task 1.
- Task 4
Keep the file list in a mktemp file that a trap deletes
Keep the list of empty files in a temporary file that is always deleted.
1. Remove the fixed name /tmp/clean-list.txt from the script.
2. Near the top, make a new temporary file with mktemp. Save its name in a variable.
3. Right after that, add a trap on EXIT that deletes that file. Write trap and EXIT on the same line.
4. Write the list into the temporary file, and read it back from there.
To pass: the script uses mktemp and a trap on EXIT, and /tmp/clean-list.txt is gone. No temporary file is left after a normal run, or after a run on a folder that does not exist.
- Task 5
Clean the real uploads/ folder with your fixed script
Now clean Tapri's real uploads folder with your fixed script.
1. Do this last, when tasks 1 to 4 pass. It changes the real files.
2. In the chapter folder, type ./clean-uploads.sh uploads, with nothing after it.
3. Run ls uploads to see what is left.
To pass: uploads/ has exactly chai menu.pdf, order notes.txt and photo.jpg, and nothing else. You ran ./clean-uploads.sh uploads in the chapter folder, and it worked. A command with a typing mistake or an error does not count, so type it again.
What you will learn
- Names that survive: find and while IFS= read -r, one whole name per line.
- Strict mode: set -euo pipefail, so a script stops at the first failure.
- Functions and local: a named block of work whose variables stay inside it.
- mktemp and trap: a temporary file of your own, removed however the script ends.
More in Shell scripting
Build "Safer scripts" 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.