1 Reply Latest reply on Jun 19, 2018 7:56 PM by Alanc-Oracle

    The which logic is behaving differently on Solaris 11.4.0.3.0

    3726563

      Hi all,

       

      When I use the logic [ -x `which inexistant` ] && echo $?  it should return 1.

       

      You can see below an example:

       

      tech@SolarisTestVM:~$ uname -a 

      SunOS SolarisTestVM 5.11 11.4.0.3.0 i86pc i386 i86pc

      tech@SolarisTestVM:~$ sh 

      tech@SolarisTestVM:~$ [ -x `which inexistant` ] && echo $? 

      which: no inexistant in (/usr/bin:/usr/sbin)

      0

      tech@SolarisTestVM:~$ 

      tech@SolarisTestVM:~$ which inexistant 

      which: no inexistant in (/usr/bin:/usr/sbin)

      tech@SolarisTestVM:~$ echo $? 

      1

       

      When I try the command “[ -x `which inexistant` ] && echo $? ”  on another Solaris version it works properly.

       

      Can someone let me know if this issue is related to this version?

       

      I'm open to any suggestions.

       

      Your help will be greatly appreciated,

      Octavio

        • 1. Re: The which logic is behaving differently on Solaris 11.4.0.3.0
          Alanc-Oracle

          The which command was replaced with the GNU version in Solaris 11.4, but it still returns the expected return codes:

          $ which ls ; echo $? ; which inexistant ; echo $?
          /usr/bin/ls
          0
          which: no inexistant in (/usr/bin:/usr/sbin)
          1

          Even in backticks it gets the right value:

          $  `which inexistant` ; echo $?
          which: no inexistant in (/usr/bin:/usr/sbin)
          1

          The difference seems to be when -x is used:

          Solaris 11.3:

          $  [ -x `which inexistant` ] ; echo $?
          1

          Solaris 11.4:

          $ [ -x `which inexistant` ] ; echo $?
          which: no inexistant in (/usr/bin:/usr/sbin)

          And that seems to be because in 11.3 the error message went to stdout, while in 11.4 it's going to stderr:

          Solaris 11.3:

          $ echo _`which inexistant`_
          _no inexistant in /usr/bin /usr/sbin_

          Solaris 11.4:

          echo _`which inexistant`_
          which: no inexistant in (/usr/bin:/usr/sbin)
          __

          So this works the same on both:

          [ -x `which inexistant 2>&1` ] && echo $?

          Though that's really checking if you can execute the error message when which fails, and a more correct version would be something like:

           

          x=`which inexistant 2>&1` ; [[ $? == 0 && -x $x ]] && echo $?