Skip to content

Commit a6fcf4b

Browse files
hellodwordakinomyogayedayak
committed
fix: prefer using iproute2 instead of ifconfig
For `_comp_compgen_available_interfaces`, we prefer to use ip (iproute) since long interface names will be truncated by ifconfig (respective packages in the operating system, e.g. inetutils) [1]. Even for the other functions that use "ifconfig" and "ip", we change to use `ip` because `ip`'s behavior is more uniform among the systems and also `ip` is becoming more common in Linux distributions. [1]: https://github.com/scop/bash-completion/pull/1090/files Co-authored-by: Koichi Murase <myoga.murase@gmail.com> Co-authored-by: Yedaya Katsman <43016107+yedayak@users.noreply.github.com>
1 parent 529aff8 commit a6fcf4b

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

bash_completion

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ _comp_compgen_mac_addresses()
16281628
# - ip link: link/ether
16291629
_comp_compgen -v addresses split -- "$(
16301630
{
1631-
LC_ALL=C ifconfig -a || ip -c=never link show || ip link show
1631+
ip -c=never link show || ip link show || LC_ALL=C ifconfig -a
16321632
} 2>/dev/null | command sed -ne \
16331633
"s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($_re\)[[:space:]].*/\1/p" -ne \
16341634
"s/.*[[:space:]]HWaddr[[:space:]]\{1,\}\($_re\)[[:space:]]*$/\1/p" -ne \
@@ -1699,7 +1699,7 @@ _comp_compgen_ip_addresses()
16991699
local PATH=$PATH:/sbin
17001700
local addrs
17011701
_comp_compgen -v addrs split -- "$({
1702-
LC_ALL=C ifconfig -a || ip -c=never addr show || ip addr show
1702+
ip -c=never addr show || ip addr show || LC_ALL=C ifconfig -a
17031703
} 2>/dev/null |
17041704
command sed -e 's/[[:space:]]addr:/ /' -ne \
17051705
"s|.*inet${_n}[[:space:]]\{1,\}\([^[:space:]/]*\).*|\1|p")" ||
@@ -1733,9 +1733,12 @@ _comp_compgen_available_interfaces()
17331733
if [[ ${1-} == -w ]]; then
17341734
iwconfig
17351735
elif [[ ${1-} == -a ]]; then
1736-
ifconfig || ip -c=never link show up || ip link show up
1736+
# Note: we prefer ip (iproute2) to ifconfig (inetutils) since long
1737+
# interface names will be truncated by ifconfig [1].
1738+
# [1]: https://github.com/scop/bash-completion/issues/1089
1739+
ip -c=never link show up || ip link show up || ifconfig
17371740
else
1738-
ifconfig -a || ip -c=never link show || ip link show
1741+
ip -c=never link show || ip link show || ifconfig -a
17391742
fi
17401743
} 2>/dev/null | _comp_awk \
17411744
'/^[^ \t]/ { if ($1 ~ /^[0-9]+:/) { print $2 } else { print $1 } }')" &&

test/fixtures/shared/bin/ip

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
3+
# Dummy "ip addr show" and "ip link show up" emulator
4+
5+
for arg in "$@"; do
6+
case "$arg" in
7+
link)
8+
cat <<EOF
9+
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
10+
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
11+
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default
12+
link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff link-netnsid 0
13+
EOF
14+
exit 0
15+
;;
16+
addr)
17+
cat <<EOF
18+
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
19+
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
20+
inet 127.0.0.1/8 scope host lo
21+
valid_lft forever preferred_lft forever
22+
inet6 ::1/128 scope host
23+
valid_lft forever preferred_lft forever
24+
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
25+
link/ether 33:33:33:33:33:33 brd ff:ff:ff:ff:ff:ff link-netnsid 0
26+
inet 192.168.80.11/24 brd 192.168.80.255 scope global eth0
27+
valid_lft forever preferred_lft forever
28+
inet6 fe80::000:0000:0000:0000/64 scope link
29+
valid_lft forever preferred_lft forever
30+
EOF
31+
exit 0
32+
;;
33+
esac
34+
done
35+
36+
exit 1

0 commit comments

Comments
 (0)