Code:
ADDRESS="'ifconfig |grep 'addr:' |cut -f2 -d ":"'"
a) What is the " supposed to do here?
b) Instead of ' (apostroph), I guess you might wanted to use ` (backtick), which is not a good idea, because it might easily get confused with apostroph. Instead use $(...)
Code:
ADDRESS=$(ifconfig |grep 'addr:' |cut -f2 -d ":")
c) In your locale, 'addr:' might work. In mine not - I would need to use 'Adresse:'
Code:
ifconfig |grep 'Adresse:'
leads to 4 addresses:
inet Adresse:192.168.32.32 Bcast:192.168.32.255 Maske:255.255.255.0
inet6-Adresse: fe80::20c:f1ff:fe59:da1/64 Gültigkeitsbereich:Verbindung
inet Adresse:127.0.0.1 Maske:255.0.0.0
inet6-Adresse: ::1/128 Gültigkeitsbereich:Maschine
d) Specifying the interface reduces this
Code:
ifconfig eth0 | grep 'addr:'
e) just the IPv4-adress:
Code:
ifconfig eth0 | grep 'inet addr:'
Code:
ADDRESS=$(ifconfig eth0 | grep 'inet addr:' | cut -f2 -d ":")
echo $ADDRESS
192.168.32.32 Bcast
let's use sed instead:
Code:
ADDRESS=$(ifconfig eth1 | grep 'inet addr:' | sed 's/[^:]*://;s/ .*//g' )
echo $ADDRESS
192.168.32.32
Of course that's not a number we can perform arithmetik on.
You have to extract the last part of the number, substract one from that, and append that to rest of the adress.
echo ${ADDRESS//\./ }
192 168 32 32
Code:
#!/bin/bash
#
function ipMinusOne {
v=$4
echo $1.$2.$3.$((v-1))
}
ADDRESS=$(ifconfig eth1 | grep 'inet addr:' | sed 's/[^:]*://;s/ .*//g' )
# echo $ADDRESS
ipMinusOne ${ADDRESS//\./ }
192.168.32.31
voila!
don't visit my homepage: