[issue1284] check4progs() add option to operta in silent mode
Frank Terbeck
bts at bts.grml.org
Sun Nov 17 12:13:03 CET 2013
Frank Terbeck <ft at grml.org> added the comment:
Hey,
Thilo Six wrote:
> i am again. Hope you still like reading mail from me. ;)
> Frank suggested using shell builtin instead of '\which'.
>
> To my experience this noticeable speeds thinks up, which ;)
> is logically since changing '\which foo' to 'builtin which foo'
> reduces filesystemseeks by 50%.
> Since 'builtin which foo' is zsh specific this resulted in quite a big patch
> linewise.
> So if you are interested here is the updated patch for check4progs().
Yeah, also: fork()ing for external programs is much more expensive then
calling built-in programs.
I think I once wrote a small utility for one of our scripts, that just
checked $PATH and didn't call which, type or whatever at all. I can't
remember which script that was though.
So here's another version:
#+BEGIN_SRC shell-script
check4progs() {
local RTN=0
local ARG
local oldifs="$IFS"
local d found
while [ $# -gt 0 ]; do
ARG="$1"
shift
found=0
IFS=:
for d in $PATH; do
if [ -x "$d/$ARG" ]; then
found=1
break
fi
done
IFS="$oldifs"
# check for availability
if [ "$found" -eq 0 ]; then
printf "%s: binary not found\n" "$ARG" >&2
RTN=1
fi
done
# return non zero, if at least one prog is missing!
return $RTN
}
#+END_SRC shell-script
That should be fairly portable (the ‘local’ instructions are not
strictly POSIX, but at least debian demands its /bin/sh to support it)
and avoids special-casing for different shells.
Feel free to benchmark this against your versions. Calling a built-in
_might_ be a bit quicker then looping over $PATH entries, but I don't
think it will make much of a difference, unless $PATH has a _lot_ of
entries.
In any case, I think generality would trump the last bits of performance
(I'm pretty positive iterating over $PATH would still beat calling an
external program). Because, if performance is the most important
measurement for something, implementing a solution as a shell-script is
wrong anyway. :-)
Note that I just typed this into my mail-client. I didn't even test if
it works, nor did I perform any bench-marking. But I think it'll give
you an idea, of what a general solution could look like.
Regards, Frank
----------
assignedto: ft
files: script-functions, script-functions
messages: 4670, 4671, 4672, 4673, 4674, 4675, 4676, 4677, 4678, 4679, 4682
nosy: Xk2c, ft
priority: wish
status: chatting
title: check4progs() add option to operta in silent mode
_____________________________________
GRML issue tracker <bts at bts.grml.org>
<http://bts.grml.org/grml/issue1284>
_____________________________________
More information about the Bugs-changes
mailing list