0

I've found what it seems to be a nice piece of code meant to rotate postfix IP but I really can't get it to work. when I run it I get this error:

rotate_postfix_ip.sh: 2: Syntax error: "(" unexpected

I have no idea how to fix it, could anyone help me please?

#!/bin/bash
ips=("64.250.120.128" "64.250.121.241" "64.250.121.242")
length=${#ips[@]}
pos=`cat current.txt`
oldip=${ips[$pos - 1]}
if [ "$length" -eq "$pos" ] ; then
    echo "limit reached"
    pos=1
else
    echo "increment!"
    (( pos++ ))
fi

echo "$pos" > current.txt

newip=${ips[$pos - 1]}
echo "prev: $oldip"
echo "new: $newip"

sed -ie "s/smtp_bind_address=$oldip/smtp_bind_address=$newip/g" /etc/postfix/main.cf > /dev/null
service postfix reload






x@debi:~$ cat test.sh
#!/bin/bash
ips=("1" "2" "3")

x@debi:~$ sh test.sh
test.sh: 2: Syntax error: "(" unexpected
x@debi:~$ bash -xv test.sh
#!/bin/bash
ips=("1" "2" "3")
+ ips=("1" "2" "3")

damn, it's not something with the new line endings. I've edited a new file with vim and it yields the same error.

  • What bash version are you using? Works fine with GNU bash, version 4.1.10(4)-release (i686-pc-cygwin). – mpy Mar 14 '13 at 10:45
  • Try to insert declare -a ips after the shebang line. – mpy Mar 14 '13 at 10:47
  • @mpy GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu), also tried with the declare -a and it still didn't work – sathia Mar 14 '13 at 10:54
  • No messed up line endings, i.e. \n\r instead of \n? Probably bash -xv rotate_postfix_ip.shwon't give any new hints? Also please try entering ips=("64.250.120.128" "64.250.121.241" "64.250.121.242") at the (bash-) command line. – mpy Mar 14 '13 at 11:00
  • 1
    How do you execute the script? If you do sh /path/to/rotate_postfix_ip.sh it won't run, because bash, when invoked as sh, mimics the Bourne shell, which doesn't support arrays. You must run the script either like this: bash /path/to/rotate_postfix_ip.sh or like this: chmod 755 /path/to/rotate_postfix_ip.sh; /path/to/rotate_postfix_ip.sh. – jaume Apr 5 '13 at 7:37
1

Just Run the script with "bash" command. it would solve the problem,

for example : bash rotate_postfix_ip.sh

| improve this answer | |
1

That's why I always prefer portable POSIX solutions that always works...

#!/bin/sh

ips='
    64.250.120.128

    64.250.121.241
    64.250.121.242
'
#####################################################
[ ! -f './current.txt' ] && echo '1' > ./current.txt

nIPS=0
for ip in ${ips}; do nIPS=$(( nIPS + 1 )); done

pos=$(cat ./current.txt)
counter=1
for oldip in ${ips}; do
    [ $pos -eq $counter ] && break
    counter=$(( counter + 1 ))
done

[ "$nIPS" -eq "$pos" ] && {
    echo "limit reached"
    pos=1
} || {
    echo "increment!"
    pos=$(( pos + 1 ))
}

echo "$pos"  > current.txt

counter=1
for newip in ${ips};do
    [ $pos -eq $counter ] && break
    counter=$(( counter + 1 ))
done

echo "prev: $oldip"
echo "new: $newip"

sed -ie "s/smtp_bind_address=$oldip/smtp_bind_address=$newip/g" /etc/postfix/main.cf > /dev/null
service postfix reload
| improve this answer | |

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.