我正在编写一个脚本,让您可以选择要使用的界面。
我需要一种方法来获取接口(interface)并将每个接口(interface)存储在一个变量中。
这是我的代码,但它只获取接口(interface):
Interfaces=$(ifconfig | awk '{print $1}' | grep ':' | tr -d ':')
最佳答案
您只需要检查包含接口(interface)名称的行,而不是包含详细信息的行。在 ifconfig
中,细节行以空格开头;在 ip
中,接口(interface)行以数字开头。
在 bash 中,您可以使用 select
创建一个简单的菜单:
#! /bin/bash
select interface in $(ip link show | grep '^[0-9]' | cut -f2 -d:) ; do
if [[ $interface ]] ; then
echo You selected $interface
break
fi
done
或
select interface in $(ifconfig -a | grep -v '^ ' | cut -f1 -d' ') ; do
if [[ $interface ]] ; then
echo You selected $interface
break
fi
done
关于linux - 获取换行符的数量并将每个换行符存储在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41926397/