4 ms·
Beware, this script will hose / clobber and then silently clean up (delete) a ton of various different files if files with those names happen to already exist.
by natch 10y ago
Beware, this script will hose / clobber and then silently clean up (delete) a ton of various different files if files with those names happen to already exist. To see the exact file names you'll have to carefully pick through the script. So obviously? run it in its own directory, which is no guarantee of safety, but should be safer.
If you have a subdirectory where you run it named after your email hostname (such as "example/" for "example.com"), then it will prompt you to "overwrite the contents of the directory" and then, if you accept, it will not only overwrite the contents, it will remove the entire contents with:
cd $outdir
rm * 2>/dev/null
There's a slight violation of user expectations here. Removing and replacing the contents isn't quite the same as overwriting the contents. It may be a fine line, but it's better to err on the side of protecting the user's files, not deleting them, when deciding where to come down on that fine line.
And if $outdir is empty or not there, it tries to detect that by first doing a check for -d $outdir, but this won't save the user if $outdir gets moved aside by another process while they are reading the prompt and before the cd happens, leaving them in another directory. Hopefully the user has rm aliased to rm -i but that still won't help since the rm is being run in its own shell in the script.
I know we're not supposed to focus on the negative here on HN. I'm sure the script is awesome for whatever it does. Just be careful out there!
- LinuxBender 10y agoThat is bad. One should always use set -u on scripts that rm any files or perform any destructive actions. One can always unset that if you need to accept args further along in a script to use in a variable.
- joshka 10y agoYou seem to have a good eye for picking up problems in scripts, and enough of an interest to write about it. Have you considered submitting a PR with your suggested changes, or if you don't have time for that, perhaps an issue with your problems?
- natch 10y agoNo, because the README for this project leaves me having utterly no idea who the audience is, what it is for, why it exists, etc., so I wouldn't feel I have the right background to do a proper PR against it.
- CJefferson 10y agoI wish modern OSes made this easier. I would love to have an easy bullrtproof way of saying "give me a temp directory for writing, don't let me write anywhere else, clean up my directory after me".
- cheeseprocedure 10y agoChances are that every system this script runs on has "mktemp," and trapping exit makes it easy to clean up when things are finished. https://www.mktemp.org/manual.html https://www.mktemp.org/manual.html http://redsymbol.net/articles/bash-exit-traps/ http://redsymbol.net/articles/bash-exit-traps/
- peterwwillis 10y agoYep, makes it much simpler to write scripts like this https://github.com/psypete/public-bin/blob/public-bin/src/stage_git_app.sh https://github.com/psypete/public-bin/blob/public-bin/src/st... (run an application that's stored in git, but in a temp working directory, and clean up after)
- CJefferson 10y agoBut, how can I make super sure some combination of my mistakes, undefined variables, failed cd commands, race conditions, etc, can't lead to me clearing the wrong directory. I'm sure this is excessive paranoia, but I have never been confident to release a script that classes a directory, I'm to worked about things like subtle differences in how mktemp works on linux and mac for example.
- the8472 10y agoon linux the following might work: - enter a user and mount namespace - mount a tmpfs, e.g. over /tmp - remount everything else as readonly alternatively firejail --overlay-tmpfs <command>
- stuuuuuuuuu 10y agoA good usecase for Docker, maybe? Put the script in a container and let it run wild, then delete the container when it's done.