【发布时间】:2017-01-29 21:56:45
【问题描述】:
我正在制作一个脚本,让您可以选择要使用的界面。
我需要一种方法来获取接口并将它们中的每一个存储在一个变量中。
这是我的代码,但它只获取接口:
Interfaces=$(ifconfig | awk '{print $1}' | grep ':' | tr -d ':')
【问题讨论】:
标签: linux bash networking interface wifi
我正在制作一个脚本,让您可以选择要使用的界面。
我需要一种方法来获取接口并将它们中的每一个存储在一个变量中。
这是我的代码,但它只获取接口:
Interfaces=$(ifconfig | awk '{print $1}' | grep ':' | tr -d ':')
【问题讨论】:
标签: linux bash networking interface wifi
您只需要检查包含接口名称的行,而不是包含详细信息的行。在ifconfig 中,详细信息行以空格开头;在ip 中,接口行以数字开头。
在 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
【讨论】:
PS3='Select an interface: '。