Which of the following commands should be used in a bash script that needs a variable containing the IP address of the eth0 interface?

Which of the following commands should be used in a bash script that needs a variable containing
the IP address of the eth0 interface? The output for the command ifconfig eth0 is shown below:
eth0 Link encap:Ethernet HWaddr 00:0C:29:CB:FA:30
inet addr:192.168.246.11 Bcast:192.168.246.255 Mask:255.255.255.0 UP BROADCAST
RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4721 errors:0 dropped:0 overruns:0 frame:0 TX packets:3216 errors:0
dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:445184 (434.7 Kb) TX bytes:512968 (500.9 Kb) Interrupt:185 Base address:0x1080

Which of the following commands should be used in a bash script that needs a variable containing
the IP address of the eth0 interface? The output for the command ifconfig eth0 is shown below:
eth0 Link encap:Ethernet HWaddr 00:0C:29:CB:FA:30
inet addr:192.168.246.11 Bcast:192.168.246.255 Mask:255.255.255.0 UP BROADCAST
RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4721 errors:0 dropped:0 overruns:0 frame:0 TX packets:3216 errors:0
dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000
RX bytes:445184 (434.7 Kb) TX bytes:512968 (500.9 Kb) Interrupt:185 Base address:0x1080

A.
IP=LANG= ifconfig eth0 | awk ‘{ print $2 }’ | cut -f2

B.
IP=`LANG= ifconfig eth0 | grep inet | cut -d: -f2 | awk { print $1 }`

C.
IP=`LANG= ifconfig eth0 | awk ‘{ print $3 }’`

D.
IP=$(LANG= ifconfig eth0 | grep inet | awk ‘{ print $2 }’ | cut -d: -f2)

E.
IP=$(LANG= ifconfig eth0 | grep inet | cut -d: -f2)



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Micha

Micha

B seems to be correct too.

Marcos

Marcos

B is almost correct, awk needs quotes, example: awk ‘{ print $2 }’

Marcus

Marcus

D.
IP=$(LANG= ifconfig eth0 | grep inet | awk ‘{ print $2 }’ | cut -d: -f2)

D.
IP=$(LANG= ifconfig eth0 | grep inet | cut -d: -f2)

Jerry

Jerry

IP=$(LANG= ifconfig eth0 | grep inet | cut -d: -f2) is not correct, as it shows the IP with the addition ‘ Bcast’ (e.g. 192.168.246.11 Bcast)