LinuxQuestions.org
Review your favorite Linux distribution.
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 07-30-2009, 09:22 AM   #1
drumbum
LQ Newbie
 
Registered: Jul 2009
Posts: 1

Rep: Reputation: 0
variable from text file bash


hello, Im sorry if this is a newbie question but Im just working on a little script to make things a little easier on my computer...

I want autodetect my wireless interface and send "wlan0" to $interface and use it through out the whole script. I know it would probably be easier just reading a "what is your interface" | read interface but id really like to try and autodetect.

I have tried iwconfig > iw.txt to generate a text file with my wireless interface, but Im having trouble getting the information from it, I either get the whole file when I use interface$=(cat /root/iw.txt) or sometimes nothing at all, I really just want to read the first word which is wlan0 and send that to $interface

I dont something similar with the mac address
mac=$(/sbin/ifconfig $interface | awk '/HWaddr/{print $NF}) but Im not getting the same result when Im applying it too iwconfig???

Sorry if english arnt clear in my first post....
 
Old 07-30-2009, 09:50 AM   #2
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by drumbum View Post
I want autodetect my wireless interface and send "wlan0" to $interface and use it through out the whole script. I know it would probably be easier just reading a "what is your interface" | read interface but id really like to try and autodetect.

I have tried iwconfig > iw.txt to generate a text file with my wireless interface, but Im having trouble getting the information from it, I either get the whole file when I use interface$=(cat /root/iw.txt) or sometimes nothing at all, I really just want to read the first word which is wlan0 and send that to $interface

I dont something similar with the mac address
mac=$(/sbin/ifconfig $interface | awk '/HWaddr/{print $NF}) but Im not getting the same result when Im applying it too iwconfig???
I'm not sure I understand what you want

1. to get the name of your wireless card interface, you can
run this
grep ':' /proc/net/wireless |head -n1|cut -d':' -f1

then it should display
wlan0
or whatever

so if you want to store that into a variable, you do
interface=$(grep ':' /proc/net/wireless |head -n1|cut -d':' -f1)

2. if you need to know the essid you're connected to
you run this (as root)
iwgetid $interface

and it should display something like:
wlan0 ESSID:"some essid"

if you need to store that info into an essid variable you do:

essid="$(iwgetid $interface | sed 's?^.*ESSID:.\(.*\).$?\1?')"

3. if you need the mac address

mac=$(ifconfig $interface | grep HWaddr|sed 's?^.*HWaddr ??')
 
  


Reply



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
[SOLVED] Bash scripting, separating variable from string text emmalg Linux - Newbie 3 06-26-2009 03:31 AM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
text formatting in bash variable David the H. Programming 10 05-09-2008 12:02 PM
Getting a variable from a text-file (bash) PatrickBecks Programming 4 02-14-2008 08:52 PM
Bash read in variable length text records lynx81191 Programming 4 11-17-2007 08:53 PM

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

All times are GMT -5. The time now is 08:51 PM.

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