I Made Myself a BASH Greeter the Right Way

Posted on 3rd February 2011 in Something Daily

I am in a state of exhausted delirium right now, or maybe it’s delirious exhaustion. As a result, I decided to make myself a Terminal greeter that tells me a true statement every time I log in to BASH. I coded it in about half an hour just a few minutes ago, with great results. Some I’ve gotten so far are “robots are sick”, “cyborgs are most bitchin’”, “dolphins are the bomb”, “SNES games are nifty”, and “pizzas are radical”. It just picks a random noun and a random adjective, with the occasional modifier and the ultra-rare super secret sentence extension (it’s “to the MAXX”). Also, for some reason, I was in the mood to come up with rhyming couplets based on the ends of my sentences; I’ll tweet them @EmmettButler when I come up with them. My favorite is

I’m a jerkface in the workplace
I keep all my shirts in a shirt case

A good evening.

If I were choosing a movie to watch as the world was ending, I’d have to go with “2001: A Space Odyssey”.

Glamour shot, activate!

MacBook Pro Backlit Keyboard in Ubuntu Maverick

Posted on 4th January 2011 in Tutorials

I was browsing around a little while ago and it occurred to me that the keyboard backlight on my MacBook Pro 5,4 wasn’t working under Ubuntu 10.10 Maverick Meerkat. I found a few resources online to help with the problem, and it ended up teaching me a lot. I wrote a script that changes the numeric string stored in /sys/class/leds/smc::kbd_backlight/brightness, then set the script to be run every time the keylight increment/decrement buttons on the keyboard are pressed. Here are the steps I took, in case you want to try this for yourself.

I worked up a shell script that, depending on the string passed from the command line, either increments, decrements, sets to zero, or sets to 100% the value of the backlight brightness.

Go ahead and use the code, or write your own, I don’t care. You can either copy and paste from here into a file called keylight in /usr/bin, or download the file here.

Once you have /usr/bin/keylight on your system, run sudo chmod +x /usr/bin/keylight to make the script executable. You’ll know that you forgot this step if you get a “command not found” error when you try to run it.

To test the script, run sudo keylight full. The keyboard backlight should come on. To turn it off, run sudo keylight off. I use an alias to avoid the necessity of sudoing every time – that is, I added the line alias keylight='sudo keylight' to the /home/emmett/.bashrc file. Still, a password is required when running the script. Since we want this to be controlled with the keyboard, we have to override that somehow. This can be accomplished by adding the following lines to /etc/sudoers (run the command sudo visudo to edit this file).

Cmnd_Alias KEY = /usr/bin/keylight
%admin ALL = (ALL) NOPASSWD: KEY

(it’s hard to see, but there is an underscore between Cmnd and Alias – that is, Cmnd_Alias)

These lines tell the computer that, when running the keylight command, members of the admin group do not need to enter a password. With that accomplished, all that’s left is to create new keyboard shortcuts for the script. In System->Preferences->Keyboard Shortcuts, click “Add”. Fill the “Name” field with something like “Keylight up”, and enter sudo keylight up into the “Command” field. (Don’t forget the sudo!) Click OK, and change the hotkey for the command to the F6 key (XF86KbdBrightnessUp). Repeat the process for keylight down, keylight full, and keylight off. I use the F5 key for down, and add Ctrl for full and off.

Follow this process, and the backlight on your keyboard should work like it does under Mac OS X. At least, it does for me. Feel free to comment with feedback, comments, or additions you make to my code. Thanks for reading!

A Busy Day with Perl

Posted on 17th November 2010 in Something Daily

Wednesdays are bananas. Really just Wednesday evenings, actually, but it all sort of blends together when you stay in motion between 5 and 11 PM. I stuck myself with a bunch of homework tonight by becoming obsessed with this perl script I’m writing. Due to yesterday’s episode involving lost (or almost lost) code, I decided to seriously revamp the script I use to automatically back up my data. It was previously a BASH script that used rsync to update the home folder on two drives, as well as a few other folders in other locations for ease of use and redundancy. One of the big flaws, though, was the lack of multiple backups. In my old system, only one image of the drive was stored at any one time, making it difficult to reclaim lost files (as I discovered). Another problem with it was that BASH does not have easy support for runtime switches, which I want to use to customize the action of the script. As an added challenge for myself, I’m not using rsync anymore, but rather writing a recursive function that traverses a directory tree and copies the contents myself. It’s a bit of work, but quite educational. And to top this off, I learned that I actually sort of love Perl – the built-in support for regular expressions and the $_ variable are helping me see what an easy language it is compared to some.

But the point of all this is, I didn’t get much else done today until crunch time. I spent the day when I could have been blogging or doing homework instead working on an algorithm. Which is great, but it means I have more work right now. It also explains why I’m updating moments before midnight. I just wrote a lab in 30 minutes, and another 1-page paper is soon to follow.

We had another session tonight for our “Get Low” cover, and this time, the material that we had recorded the previous week was actually still there! It was a miracle! After tonight, we have guitar, bass, lead and backing vocals tracked. I engineered for the most part, and I sang a high backup part (with my incredible vocal range….right?). This project is starting to become a lot of fun, though. It wasn’t so much when we had to start from scratch every week, but it is now.

Apart from all this, the reddit secret santa is approaching, and I can’t wait to be paired with someone. Also, I’m seeing mc chris for the first time on Tuesday at the Knitting Factory. It’s going to be a completely rad show, you know it’s true. In case you don’t know mc chris, here’s a starter. It’s called “Robotussin“.

Back up your data, please – and keep multiple backups

Posted on 16th November 2010 in Something Daily

I learned a very important lesson about electronic data today.

I’m crazy about my backups. I typically back up my data about once a week using a command line script that I wrote to circumvent the use of Time Machine. At any time, two separate physical drives and my laptop’s internal hard drive contain redundant copies of all of my important data. I keep the last backup on the drives until it’s replaced by the current one. As it turns out, this last part is not a very good idea.

I recently started using Dropbox to move files easily between my three different filesystems (Mac on Pro and MacBook, and Linux on the MacBook partition). It’s quite a convenient service, but it has one interesting behavior that I learned to watch out for. Typically, when you click and drag a file or folder into a new location, Finder’s default behavior is to copy the file to its new location, keeping the original intact. However, for whatever reason, the Dropbox folder behaves differently, relocating dragged folders and files instead of copying them. You have to manually copy from the local location to the Dropbox if you want two copies.

I’ve been working on a computer game for a little while now, and I have 2000+ lines of Java that drive it, combined with font and image data that I created myself. I typically work on the code/images in multiple locations, which is actually one of the main reasons I started using Dropbox. I checked on my local drive for the first time in a while to see what state my code was in, and, as you may have been able to guess, it wasn’t there. Not anywhere. My backup drives didn’t have it either. Apparently I’d done the last backup after inadvertently deleting my code by dragging it into the Dropbox – so I had no hard copies left. At this point, about an hour ago, I had resigned myself to the dozens-of-hours project being over.

Enter the soon-defunct cloud storage service drop.io. I used it before Dropbox, and made the switch mainly because of the impending end of their company after a talent acquisition by Facebook. Having used drop.io to move around the source code for my game a few times, I figured that was my last hope in finding it again. My account password had been deactivated, though, so my attempts to login were in vain. It also wasn’t helping that the drop.io homepage is all but shut down – they don’t allow any new drops to be created, for one thing. After a bit of digging through my mail trash, though, I managed to find the confirmation email I got from drop.io when I registered the drop – complete with the original drop name and password. Those worked, and luckily the compressed source was still there.

Obviously, a lot of things could have gone wrong. Like my computer automatically emptying my mail trash folder, or drop.io going offline sooner that expected, or me having previously removed the code from the drop. Needless to say, I’m very relieved that I managed to get the code back. Thank goodness for cloud storage.

So what did I learn from this? Well, mainly I learned to be careful when I copy stuff to the Dropbox. But I also have already modified my backup script to include the folder that contains code for any project in six redundant locations, just in case one (or five) get corrupted somehow. Also, I’m going to start saving two backups at a time instead of only the most recent one. That way, it’ll be possible to look at a greater span of time and hopefully recover some files that way. That certainly would have helped in this instance.

-&-

Check out this “wall PC” that somebody of the internet built. I’m jealous.