0

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?

  • Your shebang is wrong. #!/bin/sh What is BRIDGES, $RSTP etc? This is not the whole script. Also add set -x just beneath #!/bin/sh – Valentin Bajrami Oct 21 '14 at 11:41
  • Arrays are not supported in POSIX sh. This is not a syntax error as much as it is just using a feature that doesn't even exist there. Rewrite your script to not use arrays. – Chris Down Oct 21 '14 at 11:48
  • thx ok I'll try – Oliver G. Oct 21 '14 at 11:50
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.