Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
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
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.