Friday, July 30, 2010

Unix File Redirection: How to Overwrite an Existing File

I have set my tc shell settings so that when ever I try to redirect command output to an existing file, it gives me an warning. The reason for why I am using this option is that while redirecting to a file, you loose all the contents of the file. If you make a mistake while typing the file name, this can lead to very dangerous outcome. Because that file may contain data whose backup you don't have.

But, this option has some drawbacks also. That is when you want to rewrite a file by redirect to it. The problem is every time you try to do this, you are given an warning. What you have to do is to remove the file first, then redirect to that file. Two steps become involved which eats many time. But, today I have found that there is a way to perform these steps by just one. It is adding the "!" sign at the end of ">" sign. Here is the example.

% echo hello world! > test.txt
% cat test.txt
hello world!
% echo again hello world > test.txt
test.txt: File exists.
% echo again hello world >! test.txt
% cat test.txt
again hello world!


In this example, I first redirect the output of the command "echo" to file "test.txt". As "test.txt" file does not exist, shell creates a new file and writes the output to this file. Next, I redirect the output of another "echo" command to this same file and this time, shell gives me an warning. Now, if I do this by adding the "!" sign after ">", shell overwrites the existing file and writes the new output result to that file. This trick will surely save a lot of my time.

Wednesday, July 28, 2010

Embedding formatted code into webpage

I wanted to write a post on ruby option parser. I wanted to add some code to the post but then I realized that Google Blogger does not provide any tool for formatting code and syntax highlighting. It would be so nice to have a tool for embedding program codes into the blog. At first, I became frustrated with Blogger, then began to search for some solutions.

After some searching, I found the solution which I liked very much. Its vim's "TOhtml" feature. I liked this feature because I use vim for code writing. So, I do not have to go to any other tool. Next, it converts the code into html format which I can embed in any webpage which is great.

But, the default html from vim's TOhtml did not look very clean. It seems that the color I see in my terminal and the color I see in browser are quite different. I found this article which gives an guideline on how can we change the html output by doing some simple vim scripting. I almost followed his way completely, except that he used a different color scheme plugin for vim where I decided to change the style sheet for changing the look. Here's one example of ruby code outputed using the vim's TOhtml feature.

$VERBOSE = nil
require 'gnuplot'

outfile = "test.obj"
plot = Plot.new
x = (0..50).to_a
y = x.collect { |v| v**2 }
data = DataSet.new([x,y])
plot.data << data
plot.term "tgif"
plot.output outfile
plot.plot

Isn't it clean enough? I like it. It's easy to do. Run command in vim, copy the code and then paste in the Blogger. I am hoping to post articles on programming languages from now on and this method will give me a good boost.