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?

3
  • 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 Commented Oct 21, 2014 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. Commented Oct 21, 2014 at 11:48
  • thx ok I'll try Commented Oct 21, 2014 at 11:50

1 Answer 1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.