When cd fails, the script doesn't stop. It goes on in the folder it was already in:
cd failsno such folder
the script goes on
rm -f *.tmpstill in your home
the wrong files, goneand it says it worked
Stop it with || exit 1: it runs only when cd fails.
text
learner@reef:~$ cat open-shop.sh
#!/bin/bash
# Usage: open-shop.sh SHOP
cd ~/shops/"$1" || exit 1
echo "$1 is open" > opened.txt
learner@reef:~$ ./open-shop.sh west
./open-shop.sh: line 3: cd: /home/learner/shops/west: No such file or directory
learner@reef:~$ echo $?
1 ← it stopped at the cd~/clear-cache.sh DIR deletes the .tmp files in DIR. Your home folder has .tmp files of its own.
- Break it on purpose. From your home folder, run
./clear-cache.sh ~/cahce, a typo, thenls. The tests give you the lost files back. - In
nano ~/clear-cache.sh, make it stop whencdfails. - Run it on the real cache:
./clear-cache.sh ~/cache.
Objectives
- Run it on a folder with a typo, and see what it deleted.
- It stops when
cdfails. - It still clears a real cache.