4 ms·
A hastily and poorly written bash script. These days I start every bash script with what I now think of as the "Brexit Options": set -eu; set -o pipefail Th
by phaemon 10y ago
A hastily and poorly written bash script. These days I start every bash script with what I now think of as the "Brexit Options":
set -eu; set -o pipefail
The key missing one in this case was `-u`. That stops the script if you have an unset variable.
This script would do some stuff, and put a new website in place, and then remove the old one. So, my bash script had the line:
rm -rf /var/www/$olddir
You can see it already. I ran it with $olddir unset. I think I had it in my head that the directory would simply not be found so that was fine. For those of you unfamiliar with bash, since olddir="", what actually ran was:
rm -rf /var/www/
Gigabytes lost (back then, a GB was a lot!). We had backups but they took hours to restore. Horrible, horrible day.
- taspeotis 10y agohttps://github.com/ValveSoftware/steam-for-linux/issues/3671 https://github.com/ValveSoftware/steam-for-linux/issues/3671
- beaconstudios 10y agoThanks for providing the line you use to avoid these errors - I'll definitely be including this in future bash scripts! It makes you think - if we had these threads more often, perhaps we'd all get to learn more about these little process changes that could avert a disaster.
- shermanyo 10y agoI love these sort of threads for exactly this reason. I always seem to walk away with a better understanding of some tool I've used for years.
- nl 10y agoI did this same thing, except mine was in effect: sudo rm -rf /home/username/something/$SOME_VAR/* by some circumstances, SOME_VAR ended up being set to a space. Turns our that rm takes a list of directories to delete, so that deleted everything from the entire server. Fortunately I had backups. But yeah.. don't do this.
- beaconstudios 10y agothat's an argument for quoting parameters if ever I've heard one!
- proaralyst 10y agoFor the record, this would also have saved you: rm -rf /var/www/${olddir:?} Which causes a specific error if olddir is unset. A good thing to do regardless of if the Brexit options are set!
- shermanyo 10y agooh wow, I've done this before too, multiple times. Deleting _all_ the backups instead of a specific one is a heart stopping moment...
- tech2 10y agoLikewise, but mine was when I was first learning Linux in the mid 90's I'd written a script to clean out /tmp (since it wasn't a virtual fs back then) at boot. Problem is that it hadn't successfully changed to /tmp but was running instead in /etc Goodbye /etc, it was nice knowing you... first I knew about it was when my box spectacularly failed to boot. However, this was _the_ best learning experience of my life. No internet (since that was my only computer at the time) gradually rebuilding /etc by hand from a root prompt.