Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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: 2: Syntax error: "(" unexpected

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

ips=("" "" "")
pos=`cat current.txt`
oldip=${ips[$pos - 1]}
if [ "$length" -eq "$pos" ] ; then
    echo "limit reached"
    echo "increment!"
    (( pos++ ))

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/ > /dev/null
service postfix reload

x@debi:~$ cat
ips=("1" "2" "3")

x@debi:~$ sh 2: Syntax error: "(" unexpected
x@debi:~$ bash -xv
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=("" "" "") at the (bash-) command line. – mpy Mar 14 '13 at 11:00
How do you execute the script? If you do sh /path/to/ 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/ or like this: chmod 755 /path/to/; /path/to/ – jaume Apr 5 '13 at 7:37

You must log in to answer this question.

Browse other questions tagged .