LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 0
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!
 
Old 04-11-2007, 09:19 AM   #2
kshkid
Member
 
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
omnio
Member
 
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.
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.

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


Reply

Tags
bash, dhcpcd, iwconfig, scripting, wpasupplicant



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:17 AM.

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