The sh solution by Brian Campbell, while noble and well executed, has a few problems, so I thought I'd provide my own bash solution.
The problems with the sh one:
- The tilde in
~/foo doesn't expand to your homedirectory inside heredocs. And neither when it's read by the read statement or quoted in the rm statement. Which means you'll get No such file or directory errors.
- Forking off
grep and such for basic operations is daft. Especially when you're using a crappy shell to avoid the "heavy" weight of bash.
- I also noticed a few quoting issues, for instance around a parameter expansion in his
echo.
- While rare, the solution cannot cope with filenames that contain newlines. (Almost no solution in
sh can cope with them - which is why I almost always prefer bash, it's far more bulletproof & harder to exploit when used well).
While, yes, using /bin/sh for your hashbang means you must avoid bashisms at all costs, you can use all the bashisms you like, even on Ubunutu or whatnot when you're honest and put #!/bin/bash at the top.
So, here's a bash solution that's smaller, cleaner, more transparent, probably "faster", and more bulletproof.
[[ -d $1 && $1 != *[^0-9]* ]] || { echo "Invalid input." >&2; exit 1; }
rm -rf ~/foo/"$1"/bar ...
- Notice the quotes around
$1 in the rm statement!
- The
-d check will also fail if $1 is empty, so that's two checks in one.
- I avoided regular expressions for a reason. If you must use
=~ in bash, you should be putting the regular expression in a variable. In any case, globs like mine are always preferable and supported in far more bash versions.