Page 1 of 1

Favourite hammers

Posted: Mon Jan 23, 2012 3:13 pm
by IntelMole
As in "... everything looks like a nail"

If you're anything like me, you have a couple of programming tools you use more often than most, or try to use over some other solution, because they're just that awesome.

Some linux-based ones to start you off:

Since I spend a fair amount of time writing scripts to co-ordinate starting/stopping processes, or rewriting someone else's script to make it a bit more generic and remove any magic, one of my favourites has to be dirname. Putting cd `dirname $0` at the top of your script means you suddenly don't have to worry about where you are when you run the script. You don't have to worry about "will it start in the right directory when I run it under cron?" It just works. Before I discovered this one, I had a giant case statement in base based on hostnames, to cd to the right location. Ugh.

The thing I try to find excuses to use more often, is inotify. Whenever we have a problem to solve that involves the filesystem, someone always decides the best way is to set up a job to run at some handwavy number of hours after the thing is supposed to have finished, and it'll all just work. Until that thing gets more complicated and you start having to move things around that you no longer understand six months later. And don't even get me started on trying to minimize periodic downtime when these things are involved. Inotify gives you an event "watch descriptor" and tells you when an event you have subscribed to happens on a directory. We recently had a task where we receive some daily data and need to copy it to another directory with a modified name, then synchronize that directory to a couple other servers. Of course, the "5 minute cron job" idea came out. Shortly after calming down, I discovered that someone has written inotifywait, an inotify interface for terminals, all packaged up in an RPM. Learning the interface, piping that into a while loop, getting the renames I needed to do on the files right, and running the rest of the logic sorted took a couple of hours from dev to running code.

Discuss :-)

Re: Favourite hammers

Posted: Mon Jan 23, 2012 4:22 pm
by just brew it!
I probably abuse the sed tool and recursive shell scripts... not quite as obfuscated as Perl, but heading there! :lol:

Re: Favourite hammers

Posted: Mon Jan 23, 2012 5:07 pm
by IntelMole
Oh man, there's a guy at work I sometimes ask for a hand with the odd task.

He is usually convinced that the answer is perl. Sometimes he is right.

Usually I give up and write it in half the time in python - another of my hammers :-D

Re: Favourite hammers

Posted: Mon Jan 23, 2012 5:39 pm
by Madman
C++, nails everything down right and popper.

Re: Favourite hammers

Posted: Mon Jan 23, 2012 5:42 pm
by Captain Ned
Estwing.

Re: Favourite hammers

Posted: Mon Jan 23, 2012 6:26 pm
by EsotericLord
This is my favorite hammer: http://wiki.guildwars.com/wiki/Vera

Re: Favourite hammers

Posted: Mon Jan 23, 2012 9:39 pm
by notfred
awk and C with maybe a bit of Bourne shell scripting and the odd sed one liners.

Re: Favourite hammers

Posted: Mon Jan 23, 2012 11:32 pm
by Buub
Bash, Python, and the big C++ hammer (along with the STL and boost attachments)!

Re: Favourite hammers

Posted: Mon Jan 23, 2012 11:59 pm
by thegleek
i script a lot... random stuff... using bash/php/perl/etc...

example of one that wget's multiple sequential files:

wgetm.sh
#!/bin/sh

if [ $# -lt 3 ]; then
        printf "..Usage: $0 urlFormat seqStart seqEnd [wget_args]\n"
        printf "Example: ./wgetm.sh http://localhost.com/logs/file01s%03d.log 1 50\n"
        printf ".Direct: wget http://localhost.com/logs/file01s{001..050}.log\n\n"
        exit
fi

urlFormat=$1; seqStart=$2; seqEnd=$3; shift 3

printf "$url_format\\n" `seq $seqStart $seqEnd` | wget -i- "$@"


Ran into this when I tried to "wget" an entire folder, and something was preventing it from downloading everything in that folder...