Archive for the 'Tips&Tricks' Category

Parsing parameters in bash – a getopt template

Thursday, August 16th, 2007

Writing some bash scripts that parse command lines, I wrote this handy template with getopt. It is easy to apply even for simplest scripts.

OPTION_SPEC="help,flag1,flag2_params:"
PARSED_OPTIONS=$(getopt -n "$0" -a -o h --long $OPTION_SPEC -- "$@")
OPTIONS_RET=$?
eval set -- "$PARSED_OPTIONS"

Parsing error or no flags

if [ $OPTIONS_RET -ne 0 ] || [ $# -le 0 ]; then usage; die; fi

while [ $# -ge 1 ]; do case $1 in --help | -h ) usage; die;; --flag1 ) FLAG1=1;; --flag2_params ) shift; FLAG2_PARAMS="$1";; -- ) shift;; * ) echo "ERROR: unknown flag $1"; usage; die;; esac shift done

No more unannotated $ns in my scripts!

Date of yesterday in bash?

Thursday, August 16th, 2007

I recently had to hack a small shell script that would read files in a directory structure generated based on the date, something like 2007/08/16. The trick was that the script would look at yesterday’s file or files generated a few days ago.

A quick search on info and here’s the magic command

FILE="...$(date -d 'yesterday' +%Y/%m/%d)"

Interestingly, you can also use things like 3 days ago, next Monday, 2 months etc. Cool!

Listing socket/network connection owners on OSX

Wednesday, July 4th, 2007

While playing with OSX I was wondering how to find out all the networks connections a particular process owns. On Linux I’d use netstat -p for this, which does not work on OSX.

It turns out that the solution is quite simple – lsof -i does the job and works on both Linux and OSX. Two other useful commands:

lsof -ai -p PID    # all connections/sockets owned by PID
lsof -i:PORT       # lists all connections/sockets with a particular PORT.

Link: Surviving traffic storms with WordPress

Wednesday, May 2nd, 2007

Interesting link on surviving traffic storms with WordPress: not that I currently need it, but maybe in the future… ;)

In a nutshell:

  1. fine tuning of Apache (adjusting #processes, keep alives and ListenBacklog to values that match your machine’s constraints).
  2. fine tuning of MySQL query caching
  3. installing WP-Cache plugin + adaptive switching on of WP-cache plugin (only in heavy-load condition)
  4. disabling some plugins (the ones that take up a lot of resources)
  5. enabling Squid caching for static content.

Adding custom firewall rules in OSX

Tuesday, May 1st, 2007

Having extensively used Linux before I found GUI configuration of OSX firewall somewhat lacking. In particular, I wanted to limit outgoing access to some IP addresses (but I can imagine you may want to play with other things as well).

I found that I could buy Flying Buttress which should allow me to do this, but I really don’t need a graphical ipfw frontend, especially the one I’d have to pay for ;-) All I needed was to write some ipfw rules and make them persistent.

Here’s what I did:

 mkdir /Library/StartupItems/CustomIPFWRules
 cd !$

Created a file called StatupParameters.plist containing:

{
  Description     = "Custom Tadek's IPFW Rules";
  Provides        = ("CustomIPFWRules");
  Uses            = ("Network");
}

Created a file called CustomIPFWRules (the name has to match the directory name) containing a simple shell script:

#!/bin/sh

. /etc/rc.common

case "$1" in
        start)

        ConsoleMessage "applying tadek's ipfw rules"
        ipfw add 2045 deny tcp from any to "ip_I_want_to_block" out
        ;;
esac

exit 0

Voila!

BTW: a useful link on playing with Firewall in OSX.

iPhoto – my experiences

Sunday, April 22nd, 2007

Being a happy owner of a Mac I decided to give iPhoto a try to manage my photos. To give a bit of a background, we have an external Gallery2 to which we export selected photos (but locally we store more photos). I also occasionally edit my photos in GIMP (each time happy that there’s such a powerful application and at the same time swearing at the user interface) and also use Panorama Tools to stitch panoramas I took. Finally, I use my own tool for geotagging of my photos if I had taken a GPS with me on my trip. Now here’s how I manage to do all this.

Rolls vs Albums

Whenever you import photos iPhoto creates a Roll for you, whether you like it or not. The idea is that iPhoto rolls correspond to film rolls, sadly, with all disadvantages of the latter. Living in 21st century, I found it immensely annoying that my hiking photos are intermixed with my balcony photos just because I forgot to download them before going for a hike. Conversely, whenever I import a stitched panorama it always appears as a “Roll XX”, even if I’d want it to be a part of my hiking roll.

Fortunately, there’s a way to manage this. Unfortunately, it’s not very intuitive:

  • Merging rolls: you can drag photos (or an entire roll) from a roll to another roll. Note that it’s not sufficient to drag it to where the photos are, you need to drag it exactly to the roll bar. Because of this, I fold the foll I want to copy to before doing it. Once the source roll is empty, it magically vanishes.
  • Creating a new roll: if you select some photos you can create a roll with File>Create Film Roll.

Under the hood: each roll corresponds to a directory on the harddisk with the roll’s name. For example, my roll called Pfaff hike would be under ~/Pictures/iPhoto Library/Originals/2007/Pfaff Hike. Modified photos will be under ~/Pictures/iPhoto Library/Originals/2007/Pfaff Hike.

Finally, deleting photos from rolls deletes them, deleting them from album does not.

Lossy re-compression and image rotation

I noticed that whenever I import photos, iPhoto rotates them and saves them in the Modifed directory. There are two problems with it:

  • duplicate disk space: iPhoto keeps both the rotated and unrotated version.
  • lossy transform: to my horror I realized that iPhoto performs a lossy rotation operation as the photos shirnk significantly (this is really shamefull as lossless 90deg rotation is not something very difficult).

The solution is to transform the photos on the CF card. It’s a bit of a nuissance as the images will be read and written to the CF card, but let be it. Initially, I thought of exittran as I used in on Linux, but I have not found it in Darwin ports. Instead, I learned that jtran will also do the job and can be installed with port install jhead (credits to donc).

Finally, here’s a magic command that rotates all photos on my CF card:

find /Volumes/EOS_DIGITAL -name '*.JPG' -exec jhead -autorot {} \;

It is important that it’s executed after the card is inserted but before the photos are imported to iPhoto.

Second, I try to avoid any editing operations in iPhoto and set an external editor to GIMP. Now if I need to change something, I double-click on the photo and I edit it in GIMP. Also in GIMP the default quality is 85 so I have to use Save As… to set higher quality. I typically use 95-97 or so ;-)

Tadek’s workflow:

To summarize, here’s what I do with my photos on import:

  1. Get myself a cup of tea.
  2. Insert CF card, wait until “Import photos dialog pops up”.
  3. Open shell and run the magic command: find /Volumes/EOS_DIGITAL -name '*.JPG' -exec jhead -autorot {} \;
  4. Run my geotagging script of on /Volumes/EOS_DIGITAL
  5. Once the rotation has finished, import the photos.
  6. Split the photos into thematic rolls.
  7. Select photos for panoramas (use “Show File” to find the file to be imported in PTGui).
  8. Stitch panoramas.
  9. Import panoramas back to iPhoto (will show as new rolls).
  10. Merge panormas rolls.
  11. Go quickly through my rolls, deleting the photos I definitely don’t want.
  12. Edit some photos in GIMP.
  13. Create an album with all the photos from my roll.
  14. Select the best photos by reordering / deleting them.
  15. Import photos to my Gallery using the great iPhotoToGallery plugin.
  16. Change album sorting to “manual”.

As simple as one button press, huh? ;-)

Avoiding temporary files on desktop on OS X

Sunday, March 4th, 2007

While working with OS X I found it immensly annoying that Firefox saves every single PDF/PPT/DOC file I ever looked at on my desktop. I am a messy person to begin with (and so is me desktop), so I don’t want any application to place random files there on top of that ;-)

What I did first was to change Preferences>Main>Save Downloaded files To and it did change the location of downloaded files, but, surprise, surprise, not the automatically downloaded ones. Having looked for help on the web I found the solution:

  1. Change the actual location of the temporary files in Safari, which in turn changes where Firefox stores its temporary files! Surprisingly the option in Safari called “Save downloaded files to” affects both the downloads and the temporary files, and the one in Firefox, called “Save files to”, affects only the files explicitly downloaded.
  2. Make Firefox delete downloaded files on exit. To do this you need to go to about:config and create a new variable called app.helperApps.deleteTempFileOnExit and set it to true. I think it only works in FF2.0

I used both solutions: (1) (don’t want to have temp files on my desktop in any case) and (2) (if I want to keep a PDF I probably save it somewhere else). It works great now!

Shell variable assigment – a stupid bug

Friday, January 19th, 2007

I recently made a really stupid mistake. I wanted to do a simple variable assignment and run a command. What I did was the following:

VAR=value command $VAR

Obviously, this doesn’t work! As this is a single command, the shell does variable expansion before the assignment takes place, so the variable is null (or whatever it was before). Obviously, the variable is set correctly within the command’s execution environment, but it’s too late then ;-)

What I should have done in this case was:

VAR=value; command $VAR

Sadly, the command did not complain about my mistake and I only realized it after a lost night of computation. Phew!

QuickTime Audio Problems

Wednesday, January 10th, 2007

I recently wanted to play the apple keynote and, to my disappointment, the keynote was silent. Tried quitting, restarting, putting my computer to sleep and waking up again…. the usual sort of things a computer literate does when something doesn’t work ;-) All in vein… Surprisingly, the QuickTime plugin in Safari seemed to work fine.

After a bit of googling, I found that the solution was to run the GarageBand and close it. w00t it worked! “Any sufficiently advanced technology is indistinguishable from magic.” (A. Clarke).

Recovering deleted photos – my experiences

Thursday, January 4th, 2007
  • (playing with the camera) Cool. You can change the format of the CF card… Whops….
  • My CF card got corrupted.
  • The photos I thought had been already uploaded to the gallery were deleted from both the CF card and the harddisk. Moreover, I already took 50 photos on the CF card after reformatting it.

Time and time again, I realize how precious my photos are, only after they are gone. The last time they were my PhD defense photos… Auch. Fortunately, they are gone, but not forever ;-)

You will find several tools for photo recovery on Google, but most of them either commercial or give you a only a “free preview”. Thinking about it, it’s a very good business model: people are very willing to swipe their card if they can see that they get their photos back. Should have written one as well ;-) Fortunately, there are also free ones, two of them I tried by myself.

PC Inspector Smart Recovery is an excellent and easy-to-use Windows application for photo recovery. It is extremely easy to use and does an amazing job. It is also completely free, but I think is fair to reward the author with a PayPal donation.

PhotoRec is a cross-platform program for photo recovery. In fact, it runs on Dos/Windows/Linux/BSD/Solaris/MacOSX, which is really impressive. It also supports a whole range of filesystems, including FAT/NTFS/Ext2/3/HFS+. It has a simple (n)curses based interface, which is a bit of a disappointment in the age of cool animated GUIs, but it’s also relatively easy to use. Also, it has an impressive range of configuration options. And it works on Linux and my Mac. I no longer need Windows! Simialrly, the program is really free (including the sourcre) and you can rewerd the author with a donation.

Both tools worked for me smoothly and (unlike some other commercial ones I tried which ran out of memory) managed to recover almost one thousand photos from my 4GB CF card, including the wanted defense photos ;-) I don’t know which of the tools is better and I recommend both (although I would lean slightly towards the PhotoRec as I don’t have Windows anymore). Also, if you don’t get what you’re looking for, you can try both of them (as a rule all image/disk recovery tools are read-only so you can try all of them many times with no risk).