cbr
cbr

Reputation: 13652

Choosing a part of a string

Basically I have the following script:

#!/bin/bash
echo "What shall we set into managed mode? (e.g. wlan0)"
read thisend
sudo ifconfig $thisend down
sudo iwconfig $thisend mode managed
sudo ifconfig $thisend up
var=$(iwconfig wlan0)

What the script does (as you see) is to set the wireless card into managed mode, but I would like it to double-check at the end of the script if it actually is set in managed mode, which I'll write some comparison system for, but for now I just want to know if it's possible to strip out everything else from the output of iwconfig wlan0 except for Mode: Managed, and write the remaining output into a new variable.

Upvotes: 2

Views: 88

Answers (2)

Alec
Alec

Reputation: 1188

var = $(iwconfig wlan0 | grep -v 'Mode: Managed')

from grep man page

 -v, --invert-match
         Selected lines are those not matching any of the specified patterns.

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249153

Use grep or sed to extract just the part you want.

Upvotes: 0

Related Questions