I’ll be honest, I’m no expert on optimizing shell scripts. I’m hoping that readers will chime in with their tips / experiences. With that being said I do have a few tricks up my sleeve from hands on experience with code optimization using other languages.
I’ve written about my Asterisk meddling before in my Asterisk AGI IP Address lookup post. This time I’m writing about an Area Code Lookup Script I wrote that was inspired by Nerd Vittles WiFi Finder. The script prompts the user to enter a 3 digit area code and then reads the results back to the user.
The tar.gz file needs to be extracted and the resulting files placed into your agi-bin. The source can be found here.
This may be a no brainer for some of you elitists out there but if you need to (I don’t ask questions) remove all .svn folders within a project there is an easy way to do so. This would also work for other directories, but my main purpose for writing this is for .svn files / folders.
find . -iname ".svn" | xargs rm -r $1
I suppose you could also use svn export to get similar functionality, but if you’ve already checked out code, you wouldn’t want to waste precious internets and do it again would you?
Not exactly a shell script, but might be handy.
C++ preprocessor "/lib/cpp" fails sanity check
In my case I had forgot to install g++. Now I feel like a complete idiot, not the first time though.
For those a little scared of the terminal using the Linux find command may seem a little daunting. To be honest though the find command really isn’t that hard to get the hang of. By effectively learning and using the Linux find command you’ll open up a whole new can of searching capabilities. You’ll increase your capabilities, boost productivity, and be more likely to find what your looking for. Alright, enough of the pep talk already and lets get to the core that is the powerful Linux find command.
I have in the past been lazy enough to not wait for a host to come back, yet need to do something when it comes back. Lets say starting up that service I never got around to creating startup scripts for, or uploading new firmware. Well, I found this little trick on NSLU2-linux.org
sudo arping -f && echo "True"
Obviously the echo “True” can be replaced with whatever you wish. A sleep may be good if you want some extra time before performing the action. Pair this with a SSH identity / key pair and you can perform the actions on the host remotely.
I’ve been trying to keep up this blog for a number of years now. Problem is, I think I over committed when I created the Shell Script Sundays column. I’ve been keeping it going as best as possible, and will continue to do so, but I’m not going to be able to do it every week. If you may have noticed, I haven’t done any on a regular basis anyway. I do however, have some queued up for every other week, but I don’t know how long I’ll be able to continue doing that. There is also, only so much you can write about shell scripts. If anyone wants to step up and write some shell scripting articles you’re more than welcome to do so.
This will be my 70th article written since I’ve started. I doubt any one will even notice anyway, if you’d like to see it make a comeback, let me know.
That is all.
When I’m making a script I’ve learned the hard way to:
- Make backups before hand
- Make backups while performing operations
- Perform mock dry runs by using echo liberally.
So next time you’re trying to do something complex and don’t want to do something goofy, stick an echo in there before running it and save yourself some time.
Image Source: http://www.flickr.com/photos/redteam/
I tried all sorts of things. Differencing the two files and using grep to grab the lines I wanted. Whatever I tried just did not produce the expected results. Thanks to a buddy I found the solution which ended up being to sort the two files before using diff.
Assuming two files exist, File_1 and File_2. File_1 containing lines with a, b, c and. File_2 containing b and d. If we want to remove b and d from File_1 because they exist in File_2 you could use something like the this:
1 2 3 4 5 6 7 8 9 10 11 12
owen@linuxblog:~$ cat File_1.txt a b c d owen@linuxblog:~$ cat File_2.txt b d owen@linuxblog:~$ diff File_1.txt File_2.txt | grep \< | cut -d \ -f 2 a c
That’s all fine and dandy until File_2.txt contains the same lines in a different order. Running the same command produces different results. See Below:
1 2 3 4 5 6 7 8
owen@linuxblog:~$ cat File_2.txt d b owen@linuxblog:~$ diff File_1.txt File_2.txt | grep \< | cut -d \ -f 2 a b c
The solution as noted above is to use sort before hand and then difference them:
1 2 3 4
owen@linuxblog:~$ sort File_1.txt >> File_1-sorted; sort File_2.txt >> File_2-sorted; owen@linuxblog:~$ diff File_1-sorted File_2-sorted | grep \< | cut -d \ -f 2 a c
Obviously the example has been simplified, when dealing with thousands of lines the sort could take a while. With that said I’m sure there are more efficient ways to achieve the same results. I wouldn’t doubt there being a command better suited to do this. Have at it in the comments.
One of the things that has irritated me about the persistent USB thumb drive installs is the “Press Enter” to continue prompt on shutdown. Luckily, if you have persistent working correctly, the fix for this is easy.
Open up /etc/rc0.d/S89casper
Search for ENTER and the shutdown string “Please remove the disc and close the tray (if any) then press ENTER: ” > /dev/console”
For me, this was on line 89. Comment out, that line and the lines down to the ending bracket for the do_stop() routine.
Reboot, and see if you have that annoying message, if not you’re good to go.
This is not really “shell scripting” but the end result is some more bash scripts in my bin directory so what the hell? It’s going in the shell script section because its Sunday. So what?
I like to listen to music on my Windows box while I work on my Linux box. Online radio and other sounds, just get in the way too much. One of the things I wanted to do for a while was remotely control my volume so I didn’t have to use my KVM to switch over to change the volume when ever anyone came in my office.
Its actually pretty easy to control your windows volume from Linux.
At first I thought, I’d create a dummy audio device, and some how map it over. Then I figured that was overkill and I’d try something a bit easier. I have SSH via Cygwin, so all I needed was a way to control the volume locally, and I could execute the command with SSH. Having no volume utilities jump at me when I looked through the Cygwin repositories I went to look for something else.
NirCmd is an awesome utility, giving me and other Windows users the ability to do things that Linux users may take for granted, you can read about it here: http://www.nirsoft.net/utils/nircmd.html after installing it, and making sure that my corporate AV didn’t throw a hissy, it was just a matter of dumping some scripts in my bin directory and chmodding them so they would run.
Here is what they look like:
Volume Down Script: ssh windowsbox -l owen -C “nir changesysvolume -2000″
Volume Up Script: ssh windowsbox -l owen -C “nir changesysvolume 2000″
Mute: ssh windowsbox -l owen -C “nir mutesysvolume 1″
Unmute: ssh windowsbox -l owen -c “nir mutesysvolume 0″
Real simple, and the mute/unmute really comes in handy for when some one walks into my office.