Reputation: 155
I am working on an embedded linux system and I need to script around wpa_cli.
The shell I'm using is busybox' inbuilt sh.
I have a function that checks if a WiFi network is already known:
is_known() {
wpa_cli list_networks | tail -n+2 | grep -e $'\t'"$1"$'\t' | cut -f1
}
If the network is known, it returns it's ID, if it is not known, it returns nothing.
This works great for networks with "normal" SSIDs. SSIDs allow special characters too, and if I name my WiFi
_-!"#$%&'()*+,./:;<=>?@[\]^`{|}~
it doesn't get recognized by above function.
wpa_cli list_networks returns:
1 _-!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~ any [DISABLED]
How to escape the SSID properly so that is_known works as expected and returns "1" in this case?
Thanks
Upvotes: 0
Views: 52
Reputation: 155
I've rewritten it like this and it works now:
is_known() {
list_known_wifi | while read line; do
network_id=$(echo "$line" | awk '{print $1}')
ssid=$(echo "$line" | awk '{print $2}')
if [ "$ssid" = "$1" ]; then
echo "$network_id"
break
fi
done
}
Upvotes: 0
Reputation: 141603
grep
compares with a REGEX, not strings. Compare literally if you intent to.
grep -Fe "$1"
Overall, the method is flawed anyway, you could potentially do grep -F " $1 "
, but then it will anyway not work with any
or [DISABLED]
. You should split the line and then string compare the second field only.
Upvotes: 1