From: Simon Ruderich Date: Sun, 14 Mar 2010 00:13:49 +0000 (+0100) Subject: lib.sh: Fix installed() for which with no output in case of error. X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=3c9f2ee5ea8c7fe57864cad38f4f6188c569fa5d;p=config%2Fdotfiles.git lib.sh: Fix installed() for which with no output in case of error. --- diff --git a/lib.sh b/lib.sh index d9b14c5..1fa3c8f 100644 --- a/lib.sh +++ b/lib.sh @@ -11,7 +11,7 @@ unset LS_COLORS # Check if the given program is installed. Returns 0 if it exists, 1 # otherwise; so it can be used in if. installed() { - which $1 2>&1 | perl -ne 'if (not m{^/}) { exit 1 }' + which $1 2>&1 | perl -e '$_ = <>; if (not m{^/}) { exit 1 }' } # Print the current OS. The following OS are supported at the moment: diff --git a/tests/lib.sh.test b/tests/lib.sh.test index e8d5053..3568c47 100644 --- a/tests/lib.sh.test +++ b/tests/lib.sh.test @@ -41,6 +41,18 @@ echo stupid which installed ls && echo ls installed installed doesnt-exist && echo doesnt-exist installed +# which with proper exit codes and no output in case of an error. +which() { + if [ $1 = ls ]; then + echo /bin/ls + else + return 1 + fi +} +echo different which +installed ls && echo ls installed +installed doesnt-exist && echo doesnt-exist installed + # Tests for generate(). echo "Simple test file for generate() using m4. diff --git a/tests/lib.sh.test.out b/tests/lib.sh.test.out index 7139144..17d7b2c 100644 --- a/tests/lib.sh.test.out +++ b/tests/lib.sh.test.out @@ -4,6 +4,8 @@ stderr which ls installed stupid which ls installed +different which +ls installed m4: generating 'tmp/test' from 'tmp/test.m4' with options '-DTEST=m4' ################################### # WARNING! DO NOT EDIT THIS FILE! #