I've got a bash script (for personal use), part of which prints a message depending on which network I'm connected to. As part of this I want to look at the ID of the currently connected wireless network.
What I'm doing is parsing the wireless name out of the output of iwconfig
and I want to print out the name, or a special message for certain networks:
SSID=`iwconfig wlan0|grep "ESSID:" | sed "s/.*ESSID:\"\(.*\)\"/\1/"` 2>/dev/null
case "$SSID" in
StaffOnly)
echo "Staff only network at Work" ;;
*)
echo "You're on a wireless network called $SSID"
esac
The second part of this (printing the name of whatever network I'm connected to) works, but the special case of being on the StaffOnly network doesn't match and falls through the other one.
I'd like to know what I'm doing wrong with the case
statement. And also if there's just a better way of doing this anyway.
The sed
command lacks trailing .*
. It should be:
SSID=`iwconfig wlan0|grep "ESSID:" | sed "s/.*ESSID:\"\(.*\)\".*/\1/"` 2>/dev/null
^^ HERE!
Without that you are leaving the end of the line in and it apparently contains some whitespace that's causing mismatch for you.
Several related notes:
The redirection should go inside the backquote:
SSID=`iwconfig wlan0|grep "ESSID:" | sed "s/.*ESSID:\"\(.*\)\".*/\1/" 2>/dev/null`
$()
is generally preferred over backquote, because it can nest:
SSID=$(iwconfig wlan0|grep "ESSID:" | sed "s/.*ESSID:\"\(.*\)\".*/\1/" 2>/dev/null)
When doing debug prints, always add some delimiters around the variable content so you see any leading and trailing whitespace.