Help with ifconfig shell script

I am new to shell scripting and trying to create a shell script which will extract and print only ip address from ifconfig command and not all the information. I have two issues here -

  1. To know active interface like eth0, wl0, etc.
  • I guess `route` command will give me interface name in last column.
    So I wrote below code -
info=`route`
echo $info

route command gives output as -

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.0.1     0.0.0.0         UG    600    0        0 wlp3s0
link-local      *               255.255.0.0     U     1000   0        0 wlp3s0
192.168.0.0     *               255.255.255.0   U     600    0        0 wlp3s0

Now I am not sure how to extract wlp3s0 from this output.
2. when I am able to get this wlp3s0 from above output, I will run -
ifconfig wlp3s0

which will give me below output -

wlp3s0    Link encap:Ethernet  HWaddr 50:b7:c3:d9:b9:df  
          inet addr:192.168.0.9  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::a5d8:9c7b:c23f:116/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:58167 errors:0 dropped:0 overruns:0 frame:0
          TX packets:44103 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:42208037 (42.2 MB)  TX bytes:8352741 (8.3 MB)

Again I would want to extract 192.168.0.9 from this output and print it.

Any help would be greatly appreciated.

How about

ifconfig | awk '$1 != "lo" {print $7}' RS=
addr:10.1.1.1

or

ifconfig | awk '$1 != "lo" {print $31}' RS= FS="[ :]"
10.1.1.1

Do you have the ip command?

ip -o -4 addr
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
2: eth0    inet 10.1.1.1/24 brd 10.1.1.255 scope global eth0\       valid_lft forever preferred_lft forever

Thank you very much for the precise code. The best I could make using your code is as below -

ip -o -4 addr | awk '{print $2 " " $4}'

it gives output as -

lo 127.0.0.1/8
enp2s0 172.29.100.54/24
virbr0 192.168.122.1/24

Do you know how can I remove /24 from the output so that it would look like below -

lo 127.0.0.1/8
enp2s0 172.29.100.54
virbr0 192.168.122.1

Thanks,
Amar

Try

ip -o -4 addr | awk '{sub (/\/.*$/, _, $4); print $2, $4}'
lo 127.0.0.1
eth0 10.1.1.1