Search code examples
linuxbashaixgetopt

getopt is not working on AIX as it does in Linux


I am using getopt in a bash script that works fine on Linux. However, when it is executed on AIX, the output is not the same:

AIX output

TEMP=$(getopt -o d:hi: --long database:,help,instance: -n ${APPL_NAME} -- "${@}")
getopt -o d:hi: --long database:,help,instance: -n ${APPL_NAME} -- "${@}"
++ getopt -o d:hi: --long database:,help,instance: -n check_database_connection -- -i /db2/db2ez1 -d EZ1 -vvv
+ TEMP='-- d:hi: --long database:,help,instance: -n check_database_connection -- -i /db2/db2ez1 -d EZ1 -vvv '

Linux output

TEMP=$(getopt -o d:hi: --long database:,help,instance: -n ${APPL_NAME} -- "${@}")
getopt -o d:hi: --long database:,help,instance: -n ${APPL_NAME} -- "${@}")
getopt -o d:hi: --long database:,help,instance: -n ${APPL_NAME} -- "${@}"
++ getopt -o d:hi: --long database:,help,instance: -n nagios -- -i /home/db2inst1 -d sample
+ TEMP=' -i '\''/home/db2inst1'\'' -d '\''sample'\'' --'

As you can see, the values in TEMP are not valid in AIX, and the script cannot continue.

Why is the reason of this problem? how can it be solved?

This is for an open source script hosted at: https://github.com/angoca/monitor-db2-with-nagios/blob/master/check_database_connection


Solution

  • The basic issue is that getopt on AIX is not the getopt from linux. The one under linux supports long options, while the one on AIX doesn't. This is the same situation on Mac OSX.

    It can be solved by not using long options as part of the available options i.e. remove the --long database:,help,instance: -n ${APPL_NAME} -- from the command line as they're not supported arguments for the AIX version of getopt.

    Alternatively you could install a copy of gnu getopt, and choose that program for argument parsing.