2010-10-21

Perl one-liner: listing command line arguments one line at a time.

You know how Java application servers have loads of command line arguments? And every once in a while you want to list all those arguments one line at a time?

In other words, you want a perl version of using tr to change spaces to newlines.

ps -ef | grep appServerName | tr '[:space:]' '[\n*]'

Here is one way. This lends itself to extending the one-liner for processing the args in some way via @F.

ps -ef | perl -lane 'print join("\n", @F) if $F[-1] =~ /appServerName/'

Here is another.

ps -ef | perl -ne 'next unless /appServerName/; s/\s+/\n/g; print;'

Amoebae and anthropomorphism.

http://www.cs.utexas.edu/users/EWD/transcriptions/EWD09xx/EWD936.html

Just saving this link for later.

Perl one-liner: a process is communicating with which hosts (using lsof)?

You want to know which hosts that a process is communicating with. You use lsof. You want a Perl one-liner to parse the output nicely for you.

In the below example, the PID of the process I am observing is 1114114.

lsof -p 1114114 -a -i TCP -P | perl -ane '$h{(split(":", (split("->", $F[-2]))[-1]))[0]}++ if /ESTABLISHED/; END {for $i (keys %h) {print "$i = $h{$i}\n";}}'

Example output:

bob.example.com = 6
lemon.example.com = 12
smart100.example.com = 60
smart101.example.com = 8
windows.example.com = 2

2010-10-03

Personal Note: NSOperation and NSOperationQueue and atomicity.

I may need to review this later.

bbum's weblog-o-mat entry about atomicity, properties, and threading.

Main point I'm getting: don't have two threads (NSOperations) operating on the same instance variables of the same object unless you really, really must, and if you're still thinking you do, then you're probably wrong.

Oh, and atomic != thread-safe != syncronized.

2010-10-02

DLOG macro for NSLog, __PRETTY_FUNCTION__, and ##_VA_ARGS__.

My DLOG macro, stolen from Mark Damon Hughes.

#ifdef DEBUG
    #define DLOG(fmt, ...) NSLog(@"%s: " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__)
#else
    #define DLOG(...)
#endif

Explanation of gcc's __PRETTY_FUNCTION__.

Explanation of __VA_ARGS__ especially the ## operator.