Array declared in shell script works for AIX but fails in Linux

Array declared in shell script works for AiX 6.1 and above but fails in Linux CentOS 7.

I have the below code for Array in my shell script that runs fine on AiX systems.

Note: on AiX it uses ksh shell while on Linux it uses non ksh shell.

set -A filelist

However, i now wish to use the same shell script on both AiX and Linux.

On Redhat Linux CentOS 7 the above code fails as below:

-bash-4.2$ set -A filelist
-bash: set: -A: invalid option
set: usage: set [-abefhkmnptuvxBCHP] [-o option-name] [--] [arg ...]

I want the array declaration to work on both AiX and Linux systems.

How is it possible to achieve ?

Write one for AIX and one for linux...
I would not be surprised if that syntax were AIX specific, anyway AIX default shell is ksh and linux bash... either you set ksh on your linux or bash on AIX, by the way when will you migrate your AIX I doubt it beeing supported now...
typeset (or set on AIX) is used in ksh whereas declare is for bash so you have no other choice to write if you want a script to work on both or more plateforms customisation according to the OS that means using tests and executing the the required code accordingly

I think you have ksh-88.
You can augment your script(s) with a wrapper function (located somewhere at the beginning):

set_A(){
typeset varname=$1
shift
if [ "$BASH" ]
then
  eval "$varname=( \"\$@\" )"
else
  eval "set -A $varname \"\$@\""
fi
}

Now you can replace each set -A by a set_A in your script.
A demo:

set_A filelist 1 2 3 4 "*"
filelist[3]="four"
printf "%s\n" "${filelist[@]}"

Output:

1
2
3
four
*

--- Post updated at 23:31 ---

BTW in Linux you can install a ksh as well. Best compatibility provides ksh-93. But you can also try mksh or pdksh.

1 Like

As you have now valid solutions to your issue AND as we are not here to do the work for you its time to close this thread knowing that you are not following the new rules