Pass Bash Variables to Awk

Bash variables can’t be passed into awk, like one can with a lot of other commands. They need to be declared using the -v option. Here’s a code snippet which shows how.

awk -v a="$mainstring" -v b="$substring" 'BEGIN  { print index(a,b) }'

The above awk command is for locating the position of the first letter of the substring cde within the main string abcdef. In this case the position is 3, which is the position of the character ‘c’ in the main string. The variables $mainstring and $substring are passed into awk by pre-declaring them as a and b respectively.

Enable Alt Key in Xterm

Normally, if you try the key sequence Alt+1 in xterm, it will output a special character. To enable the Alt key:

Add the following to the .inputrc file in your home directory. Create one if it doesn’t exist already.

set meta-flag on
set convert-meta off
set output-meta on

Add the following to the .Xdefaults file in your home directory.

XTerm*metaSendsEscape: true
XTerm*eightBitInput: false

Open xterm if its not already, and execute the following command in the xterm Window.

xrdb -load ~/.Xdefaults

Restart all xterm windows. The Alt key should work now.

Advanced Title for Linux Terminals with Meminfo, Loadavg and more

A lot of Linux Users script the PS1 variable to see different kinds of info in their Linux Prompt. However I like to keep my prompt very simple. Instead I prefer to view all that information as part of the Terminal Title. The title of my terminal can be see in the below image.

It shows the loadavg – 1.59, number of processes running currently – 208, memfree – 78208kb. Paste the following code in your .bashrc file (towards the end) to get a similar title for your terminals.

PROMPT_COMMAND='echo -ne "\033]0;`id -un`:`id -gn`@`hostname||uname -n|sed 1q` `tty | tr -d "dev/"` [`uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g"` / `ps aux|wc -l` / `cat /proc/meminfo  | /bin/grep MemFree | tr -d "MemFreekB: "`]\007"'

Infact you can script it any way you want by adding your code in between 'echo -ne "\033]0; and \007"'.

Utility to Send Commands or Data to Other Terminals (tty/pts)

I have been working on a solution to mirror the PWD of multiple terminals. As part of that I need a way to execute commands on other tty/pts. A simple echo won’t work because echo writes to the output buffer, while I need to push these commands to the input buffer of the tty/pts. I found this forum post after some googling. I adapted the code from the forum post for my purpose. Continue reading

Patch for YOURLS: WordPress to Twitter – Filter Hook to Control the URL that is Fed into Yourls

Yourls: WordPress to Twitter is a wonderful plugin which bridges yourls with wordpress. In addition to yourls statistics, I also wanted to track the clicks on the shortened link using Google Analytics. This requires addition of Google Analytics tags to the permalink URL which then gets shortened by yourls. Hence I modified the plugin code so that the URL that is fed into yourls can be modified the wordpress way – using a filter hook. Here’s the patch that I made.

diff -aurN yourls-wordpress-to-twitter/inc/core.php /home/pratikmsinha/
--- yourls-wordpress-to-twitter/inc/core.php  2010-03-09 12:37:14.000000000 -0800
+++ /home/pratikmsinha/  2010-07-01 18:23:39.000000000 -0700
@@ -61,7 +61,8 @@
  global $wp_ozh_yourls;
  $post_id = $post->ID;
- $url = get_permalink( $post_id );
+ //$url = get_permalink( $post_id );
+ $url = apply_filters('yourls_blog_post_url', get_permalink($post_id));
  if ( $post->post_type != 'post' && $post->post_type != 'page' )
@@ -71,7 +72,8 @@
  $title = get_the_title($post_id);
- $url = get_permalink ($post_id);
+ //$url = get_permalink ($post_id);
+ $url = apply_filters('yourls_blog_post_url', get_permalink($post_id));
  $short = wp_ozh_yourls_get_new_short_url( $url );
  // Tweet short URL ?
diff -aurN yourls-wordpress-to-twitter/plugin.php /home/pratikmsinha/
--- yourls-wordpress-to-twitter/plugin.php  2010-03-09 12:37:14.000000000 -0800
+++ /home/pratikmsinha/  2010-07-01 18:26:14.000000000 -0700
@@ -68,7 +68,8 @@
  if ( !$short && !is_preview() ) {
    // short URL never was not created before, let's get it now
-   $short = wp_ozh_yourls_get_new_short_url( get_permalink($id), $id );
+   $url = apply_filters('yourls_blog_post_url', get_permalink($id));
+   $short = wp_ozh_yourls_get_new_short_url( $url, $id );
  return $short;

To use the above patch, copy-paste the code into a file, lets say yourls.patch. Go into the yourls plugin directory (wp-content/plugins/yourls-wordpress-to-twitter/) and execute the following command.

patch -p1 < yourls.patch

Do ensure that you use the correct path to yourls.patch.

Equal Height Columns using a Simple Jquery Maneuver

Equal Height Columns is a widely debated subject. There are a variety of solutions for it: faux columns, fat border and negative margins, etc. I like to be a purist and hence try to do things in pure css as often as I can, but I leaned towards JavaScript for this one. The traditional way of doing it using JavaScript is to calculate heights of the main content column and the sidebar columns and then change the height of the shorter block so that it is equal to the height of the taller block. Theoretically it sounds straightforward. However the gotcha is that there are not many cross browser solutions for calculating height correctly. jQuery’s height function has a bug in Opera, needs a work around. For some reason, it wouldn’t work correctly in IE7 either. Changing the column heights isn’t smooth either and causes flickering. Continue reading

Adding Custom Post Types to Sitemap

The Google Sitemap Generator WordPress Plugin is the most widely used plugin to generate a sitemap for a wordpress site. However it doesn’t support custom types yet. Hence the Google Bot hasn’t been visiting my website as frequently coz the sitemap doesn’t have the posts I add on my custom microblog. So I modified the plugin to add support for all public non-built-in post types. Here’s the patch.

To apply the patch, go to the folder of the google sitemap generator plugin ROOTDIR/wp-content/plugins/google-sitemap-generator and execute the below command.

zcat sitemap.patch.gz | patch -p0

Do make sure you specify the correct path for sitemap.patch.gz.

If you want a more elegant solution, go here. I found it after I wrote the patch :(.

I’m adding it directly to the plugin code, hoping that the next release of the plugin would bring support for custom post types and I won’t have to worry about the patch being overwritten.

Creating Unique Temporary Files in C using mkstemp

There are multiple ways of creating unique temporary files in C, namely tempnam, tmpfile, mkstemp. mkstemp and tmpfile are more secure than tempnam. mkstemp additionally lets you specificy a template for the file name and the directory in which these temporary files should be created. Here’s a code snippet which uses mkstemp to create temporary files in the /tmp directory.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TEMPLATE_NAME "CLIXXXXXX"       /*Template Name*/
#define TEMPLATE_DIR "/tmp"             /*Directory*/

int main (void) {
    char *template;
    int templateSize, dirSize;
    int fd;

    templateSize  = strlen( (const char *) TEMPLATE_NAME );
    dirSize =  strlen( (const char *) TEMPLATE_DIR );
    template = (char *) malloc ((size_t) ((sizeof(char)) * (templateSize + dirSize + 2)));
    strncpy(template, (const char *) TEMPLATE_DIR, dirSize);
    strncpy(template + dirSize, (const char *) "/", (int) 1);
    strncpy(template + dirSize + 1, (const char *) TEMPLATE_NAME, templateSize);
    template[dirSize + 1 + templateSize] = '\0';
    if ((fd = mkstemp (template)) < 0 ) {
        return -1;
    } else {
        printf("filename - %s\n", template);
    return 0;
The template name should always end with the six characters XXXXXX

Ruby on Rails: Choose Multiple Layouts for the Same Controller

The trick to choose multiple layouts for the same controller is to define the layout as a method and then choose the layout depending on the action_name. Here’s the sample code for a controller which uses the layout site-index for the action index and the layout site for rest of the actions.

class SiteController < ApplicationController
  def index
  def about

  def help
  layout :choose_layout
  def choose_layout
    if action_name == 'index'
      return 'site-index'
      return 'site'