3 ms·
Sadly, much of this article is an example of why you shouldn't learn from other people's shell scripts. Too often shell scripts are quickly written without much
by cbsmith 13y ago
Sadly, much of this article is an example of why you shouldn't learn from other people's shell scripts. Too often shell scripts are quickly written without much attention to best practices, so they have a lot of bad habits in them. For much the same reason bad code tends to get copy and pasted about until it becomes common such common practice no one questions it.
One shouldn't use which at all. It forks an external process, it is vulnerable to PATH issues unless you specify what will surely be a non-portable path to the executable, often isn't included in chroot'd environments, and doesn't actually return an exit status on many platforms. In short: it sucks in almost all possible ways you could hope to suck.
The proper expression to use is:
command -v curl >/dev/null 2>&1
If you know you are in bash, type -P and possibly hash become acceptable alternatives, but then... why not just use command -v right?
- voltagex_ 13y agoSounds like one for my site, which I'm still not sure if it's OK to plug here.
- susi22 13y agoMore about it here: http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script http://stackoverflow.com/questions/592620/check-if-a-program...
- e12e 13y agoBut why -v ? Wouldn't just: command curl # optionally 2> /dev/null # but there really shouldn't be any # output -- so any error you probably # want to see... do what's intended (exit with non-zero return if curl isn't available as a command)? [edit: Because command runs the command by default, it outputs info with -v!. So if the command does something by default (eg: command halt) -- then: command halt #halts! command -v halt #outputs something like /sbin/halt Whops.]