Search code examples
linuxbashgnuaix

Multiple GNU-Bash commands behave differently when run on aix, any solutions


I have written a bash script on linux, and it works well, as a part of migration I moved [rather added] an aix 7.2 node to my cluster. When I tried running bash scripts on aix it failed with multiple errors on different gnu bash commands. [ps: I have installed gnu bash on this aix node, IBM calls it a toolbox made for aix, which contains a collection of open source and GNU software built for AIX IBM Systems]

For example :

- grep -oP isn't supported

- ls -h doesn't work

- getopts fails to get parameter passed and $# as well.

I am not sure if I am doing it right with just installing the gnu bash on aix. Have anyone had any experience porting bash scripts over to ssh? Are there any pointer community can suggest to get bash script work on aix?


Solution

  • The issue is that these commands are not part of bash. What you need is the GNU versions of all these utilities, that is grep and ls. As for getopts builtin, please check which version of bash you developed the script against as compared to which version you're running it against:

    $ bash --version
    GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
    Copyright (C) 2005 Free Software Foundation, Inc.
    

    That's my bash version. If your production environment has a really old version of bash, you'll need to use bourne shell scripting, instead of bash scripting (Bourne Again SHell) to ensure portable scripts.

    Edit: Plagarizing from Olivier Dulac's answer, please take a look at the POSIX page on shell command language for portable Bourne shell scripting. Do take a look at the POSIX standard page for ls and grep for portable options.

    Another Edit: See the page on AIX Toolbox for Linux for GNU variants of the standard utilities, which are installed into /usr/linux/bin

    Yet another Edit: According to pedz, this link shows better (100% compatible) replacements for the AIX Toolbox