From 3c9f2ee5ea8c7fe57864cad38f4f6188c569fa5d Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Sun, 14 Mar 2010 01:13:49 +0100 Subject: [PATCH] lib.sh: Fix installed() for which with no output in case of error. --- lib.sh | 2 +- tests/lib.sh.test | 12 ++++++++++++ tests/lib.sh.test.out | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) 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! # -- 2.45.2