LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash scripting problem! (http://www.linuxquestions.org/questions/programming-9/bash-scripting-problem-545127/)

daltocli 04-11-2007 10:15 AM

Bash scripting problem!
 
Heya,

I'm currently working on a bash script that'll simplify my laptop connecting to wireless APs, instead of running iwconfig and wpa_supplicant I'd just run something like

Code:

wirec myAP eth2
My problem is checking that the laptop has connected to the AP and also checking it has obtained an IP address. I wanted to do this in stages, so that if the check for connection fails (not connected), the script just exits.

How do I get the output of a command in an if statement, check it and split the results? I want the if statement to run /usr/sbin/iwconfig $IFACE and grep for */100 then split that to get the * result (so the script can check for > 10% connectivity).

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!

kshkid 04-11-2007 10:19 AM

return status of the previous command executed

use $?

if 0 - operation success
else failure

is that what you are looking for ?

omnio 04-12-2007 08:21 AM

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.


All times are GMT -5. The time now is 02:26 PM.