@PNDA suggested getting data from ethtool, which I like. But, I prefer piping to grep and using a more simple awk command that non-bash people can figure out faster. The time difference between the two is negligible.
Using: Ubuntu Bionic 18.04
Network interface discovery:
root@srv:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:1e:67:96:a3:97 brd ff:ff:ff:ff:ff:ff
inet 10.0.1.101/8 brd 10.255.255.255 scope global eno1
valid_lft forever preferred_lft forever
inet6 fe80::21e:67ff:fe96:a397/64 scope link
valid_lft forever preferred_lft forever
3: rename3: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
link/ether 00:1e:67:96:a3:96 brd ff:ff:ff:ff:ff:ff
ethtool output:
root@srv:~# ethtool eno1
Settings for eno1:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
MDI-X: on (auto)
Supports Wake-on: pumbg
Wake-on: g
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
Command example:
ethtool eno1 | grep "ink detected" | awk '{print $3}'
Choose your own adventure:
ethtool {{network adapter}} | grep "ink detected" | awk '{print $3}'
Output:
If there is a link:
root@srv:~# ethtool eno1 | grep "ink detected" | awk '{print $3}'
yes
If there is not a link:
root@srv:~# ethtool rename3 | grep "ink detected" | awk '{print $3}'
no