This is an example of how you can achieve that with awk+grep. I'm not an awk expert so I hope someone will post a more beautiful code.
NOTE: this is untested code; I don't have any wireless network around right now.
Code:
#!/bin/bash
IFACE="put here your interface"
# get the line with the connectivity quality
# it is assumed you will have only one one */100 in the output of iwconfig;
# however, you can change the -m parameter to get another occurence than the first.
OUTPUT=$( iwconfig $IFACE | grep -m 1 -E "[0-9]+/100" )
# replace spaces with dots not to confuse RSTART
OUTPUT=$(echo $OUTPUT | awk '{ gsub(/\ /,".") ; print }')
# get the start position and length of the */100 string
COUNTS=$(echo $OUTPUT | awk '{ print match($OUTPUT,"[0-9]+/100") ,RLENGTH }')
# convert it to an array
COUNTS_ARRAY=(${COUNTS})
# exit if no match found (probably no connection)
if [ "${COUNTS_ARRAY[0]}" = "0" ] ; then
echo "No connection?" ; exit
fi
# decrease the start by 1 to use with bash chopping
COUNTS_ARRAY[0]=$((${COUNTS_ARRAY[0]}-1))
# get the * string from */100
QUALITY="${OUTPUT:COUNTS_ARRAY[0]:COUNTS_ARRAY[1]}"
QUALITY="${QUALITY%%/*}"
# output the quality (as you said, "the * result")
echo "the connection quality is ${QUALITY}"
Quote:
Originally Posted by daltocli
If someone could suggest how to do this in a bash script if statement, and also how to check that dhcpcd has obtained an IP for that adapter, I'd be more grateful!
|
This is simple (as
kshkid said, get the exit status). For example, in a wired network, ifconfig outputs the line containing "inet addr:" only when the interface has an IP, so you can grep for that:
Code:
ifconfig eth0 | grep -q "inet addr:" && echo "I have an IP"
Good luck with your script.