0

I am writing a script to work both in bash and ksh. I have the following code

if [ -e /bin/ksh ]; then
       set -A arrayexample a b c
else
       arrayexample=('a' 'b' 'c')
fi

I have the following error message when I run it in ksh:

Syntax error at line 4:(' is not expected`

Mansuro
  • 4,558
  • 4
  • 36
  • 76
  • What version of bash are you running? – dougEfresh Jul 08 '13 at 13:48
  • 3
    `arrayexample=('a' 'b' 'c')` should work in both `ksh` and `bash`. The error you see appears to come from neither shell, but from a shell that doesn't support arrays. – chepner Jul 08 '13 at 13:48
  • @dougEfresh version 4.2.20 – Mansuro Jul 08 '13 at 13:54
  • 2
    This should work with that version. From the tags, are you running this in hp-ux? how are you invoking this script? Also, I think you want to use $SHELL or $KSH_VERSION or $BASH_VERSION variable to determine which shell you are running in. `-e /bin/ksh` just means that a file exists and is executable, but doesn't mean you are running /bin/ksh – dougEfresh Jul 08 '13 at 14:09
  • @dougEfresh I am using the scripts on both HP-UX and Redhat. On redhat ksh does not exist by default, so I have no portability problem. The error I have comes from running it on HP-UX. On Redhat it runs without problems. – Mansuro Jul 08 '13 at 14:15
  • 2
    I've read in the past about issues with ksh and HP-UX. Does `echo ${.sh.version}` return something, or just an error msg? If error msg and no versoin info, then you're using ksh88, and you'll have to search to find if/where ksh93+ is installed on your server. Good luck. – shellter Jul 09 '13 at 04:03
  • @shellter `echo ${.sh.version}` returns an error message – Mansuro Jul 09 '13 at 09:36

2 Answers2

2

As others have noted it's better to test if the current shell is ksh by testing an environment variable. The location of the executable is too subject to changes. Then, though your else clause may not need to execute on ksh, it does need to parse. PD KSH v5.2.14 complains "ksh: syntax error: `(' unexpected" when parsing your else clause, while MIRBSD KSH R43 can parse and execute that syntax without error.

Here's a function that works in either version of ksh, and bash, using eval to evade the parse problem:

# example invocation:
# A B [C D ...]
# sets B[0]=C, B[1]=D, ...
A()if [ "$KSH_VERSION" ]
   then set -A $1 "${@:2}"
   else eval $1='("${@:2}")'
   fi
gregrwm
  • 36
  • 4
0

Try executing the script with

bash script.sh

or

ksh script.sh

I suspect your script is being executed by the default shell, which in HP-UX is (I believe) the POSIX shell, which does not support arrays.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • The error happens with the korn shell, while it shouldn't read the code in the else in the first place. – Mansuro Jul 08 '13 at 14:08
  • You `if` only checks if `/bin/ksh` is present, not if it is the shell currently processing the script. Are you *sure* you are running with `ksh`? – chepner Jul 08 '13 at 14:27
  • Yes, when I try to run the script with ksh script.sh, I have the same error. The error happens when I run it in HP-UX which satisfies the condition -e /bin/ksh – Mansuro Jul 08 '13 at 14:31
  • What version of `ksh` are you using? Does `which ksh` output `/bin/ksh`? – chepner Jul 08 '13 at 14:35
  • Which ksh outputs /bin/ksh, but I don't know the version of ksh – Mansuro Jul 08 '13 at 14:45