0

Suppose this is the o/p of my ifconfig command I want to extract HWaddr value i.e 52:54:00:1f:fd:a4 value of ens3. How do I do it In shell script i am running ifconfig command and then trying to grep.. how do i gre

 ens3      Link encap:Ethernet  HWaddr 52:54:00:1f:fd:a4
              inet addr:10.54.81.88  Bcast:10.54.81.255  Mask:255.255.255.0
              inet6 addr: fd00:10:6b50:4510::5b/64 Scope:Global
              inet6 addr: fd00:10:6b50:4510::5a/64 Scope:Global
              inet6 addr: fd00:10:6b50:4510::59/64 Scope:Global
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
Abhilasha
  • 357
  • 1
  • 5
  • 19

1 Answers1

1

You can do it like below. How will it work? ifconfig -a will redirect above mac address information to grep and grep will find out the line containing word HWaddr and will redirect the out put to awk command and finally awk cut and print the 5th column from that line.

 ifconfig -a | grep HWaddr |awk '{print $5}'

after the above command your output will be:-

52:54:00:1f:fd:a4

Now a proper shell script for this:-

#!/usr/bin/bash
# Above line instruct in which shell you want this script to run. 
MAC_ADD=$(ifconfig -a | grep HWaddr |awk '{print $5}')
echo $MAC_ADD
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17