1

I have (normal user) access to a SunOS 5.10 machine.

/usr/bin/which is broken (it blocks).

Clearly something is wrong on the system, but not having root access limits drastically what I can do. I have tried copying my which from Ubuntu, but that depends on features not available on SunOS 5.10.

$ md5sum /usr/bin/which
a39bb82e9e354c5b99e9e235a53c48d9  /usr/bin/which
$ uname -a
SunOS solaris 5.10 Generic_147147-26 sun4u sparc SUNW,Sun-Fire-V210 Solaris
$ bash --version
GNU bash, version 4.3.33(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Where can I find a which command that will work on this old system?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Ole Tange
  • 35,514

2 Answers2

3

The which command is unlikely to be broken under Solaris 10. It is just a csh script1.

Should this script contents is really corrupted, you can replace it by copy pasting the following lines in it. That will restore its standard behavior.

#! /usr/bin/csh -f
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Copyright (c) 1980 Regents of the University of California.
# All rights reserved.  The Berkeley Software License Agreement
# specifies the terms and conditions for redistribution.
#
#ident  "%Z%%M% %I% %E% SMI"
#
#       which : tells you which program you get
#
# Set prompt so .cshrc will think we're interactive and set aliases.
# Save and restore path to prevent .cshrc from messing it up.
set _which_saved_path_ = ( $path )
set prompt = ""
if ( -r ~/.cshrc && -f ~/.cshrc ) source ~/.cshrc
set path = ( $_which_saved_path_ )
unset prompt _which_saved_path_
set noglob
set exit_status = 0
foreach arg ( $argv )
    set alius = `alias $arg`
    switch ( $#alius )
        case 0 :
            breaksw
        case 1 :
            set arg = $alius[1]
            breaksw
        default :
            echo ${arg}: "      " aliased to $alius
            continue
    endsw
    unset found
    if ( "$arg:h" != "$arg:t" ) then        # head != tail, don't search
        if ( -e $arg ) then         # just do simple lookup
            echo $arg
        else
            echo $arg not found
        set exit_status = 1
        endif
        continue
    else
        foreach i ( $path )
            if ( -x $i/$arg && ! -d $i/$arg ) then
                echo $i/$arg
                set found
                break
            endif
        end
    endif
    if ( ! $?found ) then
        echo no $arg in $path
    set exit_status = 1
    endif
end

exit ${exit_status}

However, a much more common reason for which to hang is an issue with one of the file systems used by the directories listed in your PATH.

That can be an unresponsive NFS mount or a local file system exhibiting issues.

Here is one way to start investigating. Run this command and see where it hangs:

csh -x /bin/which foo

Another reason why which might hang can be an issue with your .cshrc file as it is sourced at the beginning of the script.

1 It might be argued that for the very reason it is a csh script, whichis broken by design ;-)

jlliagre
  • 61,204
  • Yup. That looks very much like the csh-script on my system that does not work. – Ole Tange Dec 03 '17 at 14:10
  • 1
    The script works as designed. What doesn't is one of your file systems and you should fix it before the issue shows up elsewhere. – jlliagre Dec 03 '17 at 14:12
  • How can I fix that without root access? – Ole Tange Dec 03 '17 at 14:32
  • 1
    Not having root privileges doesn't prevent you from diagnosing the issue and at least removing the affected directories from your PATH. Answer edited with the first steps. – jlliagre Dec 03 '17 at 14:38
1

I went with:

#!/bin/sh

type "$@" | perl -pe 's/.* is (a tracked alias for )?//'

so that it can work as a drop-in replacement for which.

Thanks @OlafDietsche for the comment.

Ole Tange
  • 35,514
  • While that works around whatever your system's fundamental issue is, ignoring that fundamental issue won't fix things. What else is wrong on that system? – Andrew Henle Dec 03 '17 at 16:17