Short answer: use ksh93
on all platforms.
The set -A
syntax to assign values to an works in all versions of ksh. You only need to use the alternate typeset -a
syntax if you run your script under bash. The easiest solution would be to always run your code under ksh. All your platforms have ksh93, so just use that: on Linux, install the ksh package and use it instead of bash. Under ksh93, you can also use a=(aModeTrace false false false false)
; this also works in bash but not in ksh88 (AIX's ksh is ksh88 but there is also a
ksh93`).
If you absolutely need a script that's portable between ksh88 and bash, you can put the eval
hackery in functions. Only whole array assignment needs to be wrapped in this way, the syntax for access to individual elements, array size and individual element assignment is the same. Use the shell, rather than the operating system, to choose the implementation.
## set_array ARRAY ELEMENT0 ELEMENT1 …
## Set the variable ARRAY to contain the specified elements. Like
## ARRAY=(ELEMENT0 ELEMENT1 …), but also supply a method that works in ksh88.
if [[ -n $BASH_VERSION ]]; then eval '
set_array () { eval "shift; $1=(\"\$@\")"; }'
else eval '
set_array () { typeset __set_array_var=$1; shift
set -A $__set_array_var -- "$@"
}'
fi
(Note that in ksh, set -A "$@"
almost works, but not quite: it fails if the first element of the array begins with a dash.)
String assignments and quoting work in the same way in all shells, there is no portability issue there.
To assign the output of a command, use command substitution (again, it's portable). The only way the output is changed is that trailing newlines are removed (unlike read
which expands backslashes and trims leading and trailing whitespace).
Result="$(printf '%s%s' "${ThisLine}" "${cst_PrintfPipeOS}" |
sed -e "s/^\(.\{${SampleSize}\}\).*/\1/")"