tags:

views:

901

answers:

2

Hello, working with cygwin and writing a script to show my my current ip address minus 2. For some reason it is giving me the follwing error: ")syntax error: invalid arithmetic operator (error token is "

this is the script I am using.

$ cat test3.sh
#!/bin/bash
#
function IPADDRESS { 
    v=$4
    echo $1.$2.$3.$((v-2)) 
}
ADDRESS=$(ipconfig | grep Address | cut -f2 -d :)
# echo $ADDRESS
IPADDRESS ${ADDRESS//\./ }

any help or suggestion is apprieciated!

+4  A: 

You've actually got a couple of problems with that script. The major one is that ipconfig will output "\r" characters at the end of the line which stuffs up the calculation.

Another is that you're not allowing for multiple NICs, hence my "head -1" addition. You'll need to select which NIC you want a little more intelligently.

And finally, I stripped out the leading space (actually all spaces) from the front of the IP address.

The following works for me:

#!/bin/bash
#set -x

function IPADDRESS {
    ((v = $4 - 2))
    echo $1.$2.$3.$v
}
ADDRESS=$(ipconfig | grep Address | head -1 | cut -f2 -d: | sed 's/[ \r]//g')
echo $ADDRESS
IPADDRESS $ADDRESS

outputting:

192.168.91.7
192.168.91.5

Actually, my version output the following:

192.168.91.1
192.168.91.-1

so you'll need to watch out for that.

I'm no stranger to dummying up the output to impress/cajole management :-).

USEFUL SNIPPET:

The "set -x" at the top of the script (when un-commented) is ideal for finding these sorts of errors. It outputs each line after all the substitutions but before executing, so that you can see exactly what's going on.

paxdiablo
A: 

Hello, I tried a couple different combinations. The following worked for me. The set -x really helped. Thanks again.

#!/bin/bash
#set -x
function IPADDRESS {
    v=$4   
echo $1.$2.$3.$((v-2))
}
ADDRESS=$(ipconfig | grep Address | head -1 | cut -f2 -d: | sed 's/[ \r]//g')
IPADDRESS ${ADDRESS//\./}