3 ms·
I don't like the `require_curl` example. Here's what is done there: OK=0 FAIL=1 function require_curl() { which curl 2>&1 > /dev/null
by chrismorgan 13y ago
I don't like the `require_curl` example.
Here's what is done there:
OK=0
FAIL=1
function require_curl() {
which curl 2>&1 > /dev/null
if [ $? -eq 0 ]
then
return $OK
fi
return $FAIL
}
(`2>&1 > /dev/null` drops stdout and writes stderr to stdout—not what was meant. `which` doesn't write to stderr, so I drop that part.)
That can be shortened significantly by using the return code directly in the if branch:
function require_curl() {
if which curl > /dev/null
then
return $OK
fi
return $FAIL
}
Or by just using the return code directly:
function require_curl() {
which curl > /dev/null
return $?
}
And as it will return the return code of the last statement executed:
function require_curl() {
which curl > /dev/null
}
- fizerkhan 13y agoThanks, It is very cool. I really donot know `which` does not write to stderr.
- mordae 13y agoWhen you are not sure and don't want to waste your time investigating, you can do &>/dev/null in bash and get rid of them both at the same time.
- damncabbage 13y ago... But only in Bash 4. This doesn't appear to work with stock OS X (Bash 3).
- lelf 13y agoBetty:srcs lelf$ perl -E 'say "out"; say STDERR "err";' &> out && cat out err out Betty:srcs lelf$ GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)
- switch007 13y agoI believe "return $?" is the default behaviour if you don't have a return statement (i.e. you can require_curl || echo "no curl")
- chrismorgan 13y agoYou're right; I just wasn't quite sure of it and had to do something else for a while. Updated duly with an extra refinement.
- cbsmith 13y agoAs I mentioned elsewhere (https://news.ycombinator.com/item?id=6210440 https://news.ycombinator.com/item?id=6210440), it's just wrong to use "which" in general.
- wazari972 13y agoIndeed, which does not recognize aliases and functions, so `command -v` seems better: command -v llh >/dev/null 2>&1 ; echo $? 0 # good usr/bin/which llh >/dev/null 2>&1 ; echo $? 1 # good however (zsh) shell internal which does: which llh ; echo $? llh='ls -lh' /usr/bin/ls 0 #good (I'm not sure however when ZSH chooses to use its internal function or the one from $PATH)
- schmozby 13y agoAnd that could be reduced and generalized to require() { which $1 > /dev/null }.
- deleted 13y ago[deleted]
- noisy_boy 13y agoDepends upon the system. In my system, it does write to stderr: $ which abcdasdf 1>/dev/null which: no abcdasdf in (/usr/lib/mpi/gcc/openmpi/bin:/usr/local/bin:/usr/bin:/bin:.....snip) $ uname -srv Linux 3.4.47-2.38-desktop #1 SMP PREEMPT Fri May 31 20:17:40 UTC 2013 (3961086)
- reidrac 13y agoI'm intrigued by your uname command. What kind of information adds to this case? Honest question, I would have expected the following command to provide context: $ which --version | head -1 GNU which v2.20, Copyright (C) 1999 - 2008 Carlo Wood. Or may be the output of lsb_release, although it may not be installed in your system. Is "which" behaviour dependant on the kernel version? EDIT: formatting; also to add that which writes to stderr if the command you're asking for can't be found in the path.
- e12e 13y agoMy alterntives to a couple of these: function debug() { if [ -v VERBOSE ] then echo "${1}" # might want to add: >2 fi } function has_dep() { unset dependency dependency=$(command -v "${1}") if [ -v dependency ] then debug "found \"${1}\": ${dependency}" return 0 else debug "\"${1}\": not found" return 1 fi } # example: VERBOSE=1 if has_dep curl then echo "we're good" fi if has_dep notfoundthing then echo "not good." fi