Re: when using `which` send errors to /dev/null
On 23May2007 13:33, David Champion <dgc@xxxxxxxxxxxx> wrote:
| Is the problem with irregular behavior of different whiches?
I've never trusted it even to exist everywhere:-)
| Which is
| convenient but besides this has the drawback that since it's almost
| always a csh script, it sometimes returns csh aliases and such instead
| of what you really want.
| It's easy enough to add a which-like command though.
| where ()
Yes! I'd run with this myself.
| echo $PATH | tr : '\012' | while read dir; do
This is more robustly written:
printf "%s\n" "$PATH" | tr : '\012' | while read -r dir; do
Unless that breaks on Solaris, which it may:-(
| [ -x "$dir/$1" -a ! -d "$dir/$1" ] && echo "$dir/$1" && return 0
| ## "return 0" returns from the do/done subshell, not the
| ## function. "false" here ensures that a "where" that does not
| ## find the program returns false at end.
This is not necessary. The last loop iteration will get false from the
"read" condition of the while, and so the subshell will exit non-zero
Cameron Simpson <cs@xxxxxxxxxx> DoD#743
If you can keep your head while all those about you are losing theirs,
perhaps you don't understand the situation.
- Paul Wilson <Paul_Wilson.DBS@xxxxxxxxxxxxxxxxxxxxxxx>