fridgecow
Posts: 9
Joined: Mon Jan 07, 2013 10:09 pm

How to run code at startup under certain conditions?

Wed Jan 16, 2013 6:17 am

I want to run some code at start up, but only if there is no wlan adapter plugged in. How do I do this/detect whether my wlan adapter is in?

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: How to run code at startup under certain conditions?

Wed Jan 16, 2013 10:25 am

Don't have a ready answer, but you could look at the WLAN/Wi-FI setup threads on this forum to see what commands are used to check things - then learn about 'grep' and how to use it in a shell script to extract info from command output - you can use 'if' statements in shell scripts...
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

pjc123
Posts: 911
Joined: Thu Mar 29, 2012 3:37 pm

Re: How to run code at startup under certain conditions?

Wed Jan 16, 2013 12:09 pm

Look at the "lsusb" command; it lists all USB devices. First plug your wifi dongle in and boot the OS. Then run the lsusb command. You will see a line(s) of text which lists you wifi device. It should be obvious where it is, but if you can not tell which line(s) it is, then unplug the device and run lsusb again and note the difference between the two outputs. Now that you know what the text looks like, you can write a script and use grep, as poster "rst" mentioned, to search for that line of text (You really only have to search for a distinctive key word that doesn't exist anywhere else in the listing or that does not refer to a different device). If the text exists (or the grep returns true), then the adapter is plugged in, or conversely, if the text does not exist (or the grep returns false), then the adapter is not plugged in. Then use your "if" statement to take the appropriate action.
My Raspberry Pi Project Page:
http://www.flaminghellmet.com/launch/

necc
Posts: 25
Joined: Thu Aug 30, 2012 10:55 pm

Re: How to run code at startup under certain conditions?

Wed Jan 16, 2013 3:57 pm

#!/bin/bash
if [ -z `iwconfig | grep wlan0` ]
then
#device not present
fi

fridgecow
Posts: 9
Joined: Mon Jan 07, 2013 10:09 pm

Re: How to run code at startup under certain conditions?

Wed Jan 16, 2013 4:13 pm

necc wrote:#!/bin/bash
if [ -z `iwconfig | grep wlan0` ]
then
#device not present
fi
Thanks! It would have been nice to work out the actual code myself, but that helped!

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: How to run code at startup under certain conditions?

Wed Jan 16, 2013 4:18 pm

That is the problem here: some users want (need) specific instructions or examples 'just to get things done' - some are more willing to learn things only wanting 'pointers'...

Maybe need to start stating something along "please don't post exact code" or those who reply 'SPOILER WARNING' :lol:
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

necc
Posts: 25
Joined: Thu Aug 30, 2012 10:55 pm

Re: How to run code at startup under certain conditions?

Wed Jan 16, 2013 4:33 pm

Lol :D Apologies! Didn't mean to help too much! :D

necc
Posts: 25
Joined: Thu Aug 30, 2012 10:55 pm

Re: How to run code at startup under certain conditions?

Wed Jan 16, 2013 4:37 pm

Well, I suppose there's at least several other ways of doing that, so you can still figure out your own code :)
you could work with the aforementioned lsmod or /dev filesystem, surely something in /proc could give it away too, damn you can even write your own kernel module to detect a device with a certain ID :D
So your / is your limit :D Or is it? :P

Return to “General programming discussion”

Who is online

Users browsing this forum: Bing [Bot] and 6 guests