Howto adapt this bash construct to a sh valid construct

On converting some bash scripts to a platform where only /bin/sh is available i’m stumble about this bash construct:

#!/bin/sh

   RSTPD=/sbin/rstpd
   RSTPCTL=/sbin/rstpctl
   RSTP=0
   bridgeprefix="gbr"
   #UPDATE_STRING=-b eth2 eth3
   BRIDGES="0"
   CSIF[0]="eth2"
   SSIF[0]="eth3"

       for b in $BRIDGES ; do
            echo Starting service bridge $bridgeprefix$b
            brctl addbr $bridgeprefix$b  ||  RETVAL=1
            if [ $RSTP == 0 ] ; then
              brctl stp $bridgeprefix$b on 
              brctl setbridgeprio $bridgeprefix$b 65000
            fi

            for br in ${CSIF[$b]} ; do  #<--bad substitution
                echo Adding CSIF $br on $bridgeprefix$b
                ifup $br
                brctl addif $bridgeprefix$b $br  ||  RETVAL=1
            done
       done

I got a syntax error: bad substitution on the second for loop.

Howto adapt this bash construct to a sh compatible construct?

Answer

Given the fact your arrays only have one element and that you test a constant variable, your script, as it is written is equivalent to:

#!/bin/sh

bridge="gbr0"
CSIF="eth2"

echo Starting service bridge $bridge
brctl addbr $bridge || RETVAL=1
brctl stp $bridge on 
brctl setbridgeprio $bridge 65000

echo Adding CSIF $CSIF on $bridge
ifup $CSIF
brctl addif $bridge $CSIF ||  RETVAL=1

Leave a Reply

Your email address will not be published. Required fields are marked *