sed – Nexenta Bash Script Uses /usr/sun/bin/sed Instead of /usr/bin/sed

nexentasedsolaris

I'm using a script for Linux and for Solaris (Nexenta).

This line works on Linux, but not on solaris (but when run from shell it works):

cat "pg_hba.conf" | sed "0,/^local/{s/md5/trust/}"

The error message is:

sed: command garbled: 0,/^local/{s/md5/trust/}

After some research, I found out that the sed that bash uses in the script is different.

from shell: /usr/bin/sed

from script: /usr/sun/bin/sed

I want to make the script use /usr/bin/sed.

What I tried to do:

  1. call sed with full path. Same results. It seems that it is still uses the other sed
  2. tried to call it via bash -l. Same results.
  3. tried to declare a different command: S=/usr/lib/sed and use $S instead. Same results.
  4. checked PATH – both cmd and script have /usr/bin in it.
  5. Tried replacing the double quotes to single. Same results.
  6. tried running the sed with -r flag. Output is :

    # /usr/xpg4/bin/sed -r
    /usr/xpg4/bin/sed: illegal option -- r
    Usage: sed [-n] script [file...]
    sed [-n] [-e script]...[-f script_file]...[file...]

HELP??

What I need to do is to replace the first match of "md5" with "trust" on the first line that starts with "local"). I know I can do it otherwise but I the sed issue is itching me too much!

EDIT:

I hope this makes a little order…

  1. PATH from login shell = /usr/local/ctera/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/ctera/apache-ant-1.8.2/bin
  2. PATH from script =
    /usr/sbin:/usr/bin:/usr/local/ctera/apache-ant-1.8.2/bin:/usr/local/ctera/apache-ant-1.8.2/bin
  3. PATH from script when PATH=$(command -p getconf PATH):$PATH is called =
    /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin:/usr/sbin:/usr/bin:/usr/local/ctera/apache-ant-1.8.2/bin:/usr/local/ctera/apache-ant-1.8.2/bin
  4. truss -f sed from login shell calls /usr/bin/sed
  5. truss -f sed from script calls /usr/sun/bin/sed
  6. truss -f /usr/bin/sed from script calls /usr/sun/bin/sed !!!
  7. After setting PATH=$(command -p getconf PATH):$PATH:
    7.1 truss -f sed from script calls /usr/xpg4/bin/sed
    7.2 truss -f /usr/bin/sed from script calls /usr/sun/bin/sed !!!

MORE INFO:

Commands output: (run both from shell prompt and from within the script)

  1. truss -ft execve /usr/bin/sed q
    as shell command:
    8604: execve("/usr/bin/sed", 0x08047D20, 0x08047D2C) argc = 2
    from script:
    8545: execve("/usr/sun/bin/sed", 0x08047768, 0x08047774) argc = 2

  2. file /usr/bin/sed
    as shell command:
    /usr/bin/sed: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped
    from script:
    file: /usr/bin/sed zero size or zero entry ELF section - ELF capabilities ignored
    file: /usr/bin/sed: can't read ELF header
    /usr/bin/sed: data

  3. ls -l /usr/bin/sed
    as shell command:
    -rwxr-xr-x 1 root root 96440 May 31 2008 /usr/bin/sed
    from script:
    -rwxr-xr-x 1 root root 96440 May 31 2008 /usr/bin/sed

  4. ls -ld $(type -pa sed)
    as shell command:
    -rwxr-xr-x 1 root root 96440 May 31 2008 /bin/sed
    -rwxr-xr-x 1 root root 96440 May 31 2008 /usr/bin/sed

    from script:
    -rwxr-xr-x 1 root root 96440 May 31 2008 /usr/bin/sed

  5. md5sum $(type -pa sed)
    as shell command:
    385361c5111226c8eac8e25b53fed29c /bin/sed
    385361c5111226c8eac8e25b53fed29c /usr/bin/sed

    from script:
    385361c5111226c8eac8e25b53fed29c /usr/bin/sed

    • The script is invoced by JAVA code.

    • uname -a
      SunOS cteraportal 5.11 NexentaOS_134f i86pc i386 i86pc Solaris

    • This might add info about the sed version on my machine

      ~# ll `find / -name sed`  
      -rwxr-xr-x 1 root root 96440 May 31  2008 /usr/bin/sed  
      -r-xr-xr-x 1 root bin  35656 Sep  7  2010 /usr/sun/bin/sed  
      -r-xr-xr-x 1 root bin  32104 Sep  7  2010 /usr/ucb/sed  
      -r-xr-xr-x 1 root bin  35636 Sep  7  2010 /usr/xpg4/bin/sed  
      
      /usr/share/doc/sed:  
      total 113  
      -rw-r--r-- 1 root root   168 Jun 21  2005 AUTHORS.gz  
      -rw-r--r-- 1 root root  2507 Jun 21  2005 BUGS.gz  
      -rw-r--r-- 1 root root  6584 Feb  3  2006 NEWS.gz  
      -rw-r--r-- 1 root root   285 Jun 21  2005 README.gz  
      -rw-r--r-- 1 root root  1071 Jan 12  2006 THANKS.gz  
      -rw-r--r-- 1 root root  4806 May 31  2008 changelog.Debian.gz  
      -rw-r--r-- 1 root root 32312 Feb  3  2006 changelog.gz  
      -rw-r--r-- 1 root root   796 May 31  2008 copyright  
      drwxr-xr-x 2 root root     3 May 30  2011 examples  
      drwxr-xr-x 2 root root     3 May 30  2011 sed-4.1.5  
      -rw-r--r-- 1 root root 56538 May 31  2008 sedfaq.txt.gz  
      

Best Answer

OK, found it. That now makes sense.

the behaviour is Nexenta-specific and explained at http://lwn.net/Articles/334756/

GNU and not GNU

The default behavior of Nexenta is to prefer GNU utilities, which are installed in /usr/bin and /usr/sbin and so on. The Sun versions of these utilities are installed in /usr/sun/bin and /usr/sun/sbin. Nexenta uses a trick to be able to switch between a GNU and a SUN personality: if the environment variable SUN_PERSONALITY is set to one, the search paths /usr/sun/bin and /usr/sun/sbin take preference, even if the user executes the commands explicitly by their absolute path, e.g. /usr/bin/sed. This ensures that Solaris-based scripts work in Nexenta without modifications. Nexenta also uses this functionality in its SVR4 package commands. They can be used to install native Solaris packages in SVR4 format, calling alien to convert the package on-the-fly to a Debian package.

That's done somewhere in the libc.

So,

$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
$ SUN_PERSONALITY=1 sed --version
sed: illegal option -- version

So, your script started from Java, must have the SUN_PERSONALITY set.

You can unset that in your script if you want the GNU tools.

Related Question