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: