How to run code at startup under certain conditions?
8 posts
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?
- Posts: 5
- Joined: Mon Jan 07, 2013 10:09 pm
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'
- Posts: 852
- Joined: Thu Nov 01, 2012 12:12 pm
- Location: Dublin, Ireland
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.
- Posts: 662
- Joined: Thu Mar 29, 2012 3:37 pm
#!/bin/bash
if [ -z `iwconfig | grep wlan0` ]
then
#device not present
fi
if [ -z `iwconfig | grep wlan0` ]
then
#device not present
fi
- Posts: 23
- Joined: Thu Aug 30, 2012 10:55 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!
- Posts: 5
- Joined: Mon Jan 07, 2013 10:09 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'
Maybe need to start stating something along "please don't post exact code" or those who reply 'SPOILER WARNING'
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
- Posts: 852
- Joined: Thu Nov 01, 2012 12:12 pm
- Location: Dublin, Ireland
Lol
Apologies! Didn't mean to help too much! 
- Posts: 23
- Joined: Thu Aug 30, 2012 10:55 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
So your / is your limit
Or is it? 
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
So your / is your limit
- Posts: 23
- Joined: Thu Aug 30, 2012 10:55 pm