henryyao
henryyao

Reputation: 1818

How to get the wlan number in the linux system using BASH script

I want to write a script to enable the wireless driver.

If I input iwconfig in the terminal, it will give me the following output:

lo        no wireless extensions.

eth0      no wireless extensions.

wlan3     IEEE 802.11bgn  ESSID:off/any  
          Mode:Managed  Access Point: Not-Associated   Tx-Power=0 dBm   
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Encryption key:off
          Power Management:off

What I want to do next is to input:

ifconfig wlan3 up

Is there a way to extract the number 3 from the first output and make those into a bash script?

Upvotes: 0

Views: 2065

Answers (2)

Jiaming Lu
Jiaming Lu

Reputation: 885

iwconfig prints only wireless interfaces to stdout, other output is in fact stderr. Therefore the following command will work regardless of interface name.

$ iwconfig 2>/dev/null | grep -o "^\w*"

Upvotes: 3

Justin L.
Justin L.

Reputation: 13600

if you just want want ifconfig wlan3 up, then you don't need to isolate the 3.

you can get wlan3 with just

$ iwconfig | grep -o "^wlan[0-9]\+"
wlan3

Upvotes: 2

Related Questions