A clever use of pipes is usually the answer. For example you can use the raw
$ ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.4 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::ca60:ff:fe06:ac5b prefixlen 64 scopeid 0x20<link>
ether c8:60:00:06:ac:5b txqueuelen 1000 (Ethernet)
RX packets 622902 bytes 792538068 (755.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 392538 bytes 37764707 (36.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
And then carve away this output until you see the information you are after:
$ ifconfig eth0 | grep -w inet | cut -d ' ' -f 10
My own choice of weapons:
- Use grep with the
-w option to search for only whole word matches. This way we do not catch
inet6 as well.
cut with a delimiter (
-d) of a space and then select the appropriate field (
-f) number to get the information we are after.
There are many, many tools to do this and doubtless a better way to accomplish this particular job. But this example shows the use of pipes and several of the tools that you can use.