Sunday, December 4, 2011

Programmers' Advent Calendar: #3 How to find a program's executable (C/C++)

Say you are writing a C/C++ program and from within that program you need to find the path of the exe file. Under *nix systems. Get the pid, "int pid = getpid();" then simply the file "/proc/pid/exe" is a link to the executable. From there you can do whatever you want. You can also find a bunch of other information about your process in the "/proc/pid/" directory. Come back for more tomorrow :).

Programmers' Advent Calendar: #2 Back references with regular expressions

I am a little late writing my advent calendar posts, but I will try to keep up. For this episode, let me tell you about a really useful feature that's available in most search/replace tools (e.g. sed, ruby regular expressions ...).


Imagine this scenario: I am writing a novel whose main character and her sister are called "Sarah" and "Judy" "Williams". There are other characters called "Sarah" and "Judy" however those have other last names. There are also other characters whose last name is "Williams" but with different first names. I always refer to my characters as "FirstName LastName" in my novel.


 Now, my editor and I decided that we will change the sisters names to "Sarah" and "Judy" "Brians". And it's a disaster, because now I have to go and change the names by hand. I cannot replace "Sarah ***" or "Judy ***" by "Sarah Brians" and "Judy Brians" because I will them mess up the other names. I can't also just replace "Williams" by "Brians" because I will again mess up the other names. What do I do? One solution would be replace "Sarah Williams" by "Sarah Brians" then again replace "Judy Williams" by "Judy Brians". However, what if there were 20 characters from the same family? Back references solve this problem. 


 Back references simply select a part of the search term and save it, then I can use this same part in the replace pattern. It is very useful whenever the change location is identified by something that is bigger than the changed part. In this example, I would replace "\(Sarah|Judy\) Williams" by "\1 Brians". \1 will refer back to whatever was between "\(" and "\)". If you had more pairs of "\(" and "\)" then you can refer back to them using "\2" , "\3" ... in the order they were defined. Come back tomorrow for another little useful piece of information. :).

Saturday, December 3, 2011

Programmers' Advent Calendar: #1 Finding a path to a bash script

I have decided to start this series, for the 24 days of christmas, I will dedicate a post each day to tell you about a little thing you might not have known. I will try to stick to small examples or one liners most of them time. However, some biggers stuff will be too good to miss. This is the first post of the Series.

Often enough, you are writing a bash script and you want to find out where is your script. Here is a reliable and repeatable way to find the directory where your path resides.

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

However, if your script changes directories before calling this, it will get the wrong value. Now let me explain how the script works. BASH_SOURCE[0] will return the relative path of the script from the directory where the script was called. dirname will remove the script name from that. Which at the beginning of the script is the same as pwd. If we go to that directory then we are at the same dir as the script. Executing pwd there will give us the absolute path. And this is what we store in DIR.

Come back tomorrow for another post :).