Solaris 'which' command does not return proper exit code

Richard Lee over at VA Software brought this up to me today. He said that the Solaris 'which' command does not return the correct error code upon failure. Because of this he has to end up parsing stdout when he should be able to simply rely on the exit code.
Example:

# uname -a
SunOS sol10 5.10 s10_55 sun4u sparc SUNW,Sun-Blade-1500

# which ls
/usr/bin/ls
# echo $?
0

# which oogabooga
no oogabooga in /usr/sbin /usr/bin
# echo $?
0


As Richard would say, "Dirty".
Comments:

The Solaris which(1) is the original csh script implementation, and hasn't changed substantially since 1990. I'll file a bug for this case, although it may say "consider F/OSS implementation". In the meantime, a suggested workaround that actually uses $PATH is "ksh whence oogabooga".

Posted by Stephen Hahn on June 10, 2004 at 04:31 PM PDT #

Alternatively, use the "type" command or builtin.

Posted by Michael Davey on June 11, 2004 at 07:50 AM PDT #

Unclear that this is a bug. I chatted up one of our POSIX Standards reps (and experts). To make a long exposition short: The standards have a basic principle that as long as a utility correctly performs its function, it should exit with a 0 exit status unless documented to do otherwise (such as false). Since the man pages for which and csh do not say what constitutes correct performance, there is no guidance that I know of for what which "should" return as an exit status. So, while I can imagine that it would be more useful to call this an error, changing the specification a dozen or so years after scripts have come to rely on this behavior seems risky.

Posted by Keith Bierman on June 11, 2004 at 11:53 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

moazam

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today