Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-11-2007, 09:15 AM   #1
LQ Newbie
Registered: Mar 2007
Location: Kent, United Kingdom
Distribution: ArchLinux
Posts: 2

Rep: Reputation: 0
Bash scripting problem!


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

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!
Old 04-11-2007, 09:19 AM   #2
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
return status of the previous command executed

use $?

if 0 - operation success
else failure

is that what you are looking for ?
Old 04-12-2007, 07:21 AM   #3
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16
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.

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

# exit if no match found (probably no connection)
if [ "${COUNTS_ARRAY[0]}" = "0" ] ; then
	echo "No connection?" ; exit

# decrease the start by 1 to use with bash chopping

# get the * string from */100

# output the quality (as you said, "the * result")
echo "the connection quality is ${QUALITY}"
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:
ifconfig eth0 | grep -q "inet addr:" && echo "I have an IP"
Good luck with your script.

Last edited by omnio; 04-12-2007 at 02:32 PM.


bash, dhcpcd, iwconfig, scripting, wpasupplicant

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH scripting problem danreed007 Programming 5 04-24-2006 05:02 AM
bash scripting problem sutley Linux - Software 1 12-17-2004 11:33 AM
bash scripting problem Sammy2ooo Linux - General 3 12-13-2004 12:31 PM
BASH scripting problem deadlock Programming 5 08-15-2003 04:33 AM
bash scripting problem raven Programming 7 03-10-2003 05:48 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:54 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration