shellaix

Is there a syntax to allow empty list in for command using AIX 7.2 bin/sh


A generated script has no items in the for loop, in bash the empty list is accepted, however in /bin/sh (or /bin/ksh) on AIX 7.2 it fails.

eg. with 'test-empty.sh'

for v in 
do
  echo $v
done

$ . test-empty.sh
/bin/sh: 0403-057 Syntax error: `newline or ;' is not expected.

As expected when values are present it works, with 'test-val.sh'

for v in 1 2 3
do
  echo $v
done

$ . test-val.sh
1
2
3
$

Even attempting to end the command with empty list fails 'test-sep.sh'

for v in ;
do
  echo $v
done

$ . test-sep.sh
/bin/sh: 0403-057 Syntax error: `;' is not expected.

Is there any syntax that would allow for an empty list in these sh/ksh shells ?

I'm not easily able to change the process to use a different shell, or to change /bin/sh to another shell.


Solution

  • Neither Korn Shell nor Bourne Shell on AIX support this syntax. for loop can only have two forms:

    1. for VAR ; do ... ; done
    2. for VAR in WORD WORD ; do ... ; done

    and in form 2. there always have to be at least one WORD. If you have only choice between Bourne and Korn shells you need to workaround by storing your list to a variable and prior the loop checking if that variable contains items.

    Other option you have is to use extended Korn Shell (/usr/bin/ksh93) that ships with default AIX 7 installation and should support this feature.