Archive

Posts Tagged ‘shebang’

Interpreter Scripts

November 9th, 2009 subogero Comments off

I wanted to turn the ogc calculator into a full-blown interpreter, to be able to create an executable ogc test-script:

#!/usr/bin/ogc
# Test script for ogc.
# Invoke: "ogc Test" or simply "./Test"
# It shall produce the following output:
# 42
# 42
# 42

# 42 hex
11*3*2 x

# 42 oct
 F*2+4 o # Leading space

# 42 dec
 52 d # Leading tab

 # Leading tab and comment

This is called an executable interpreter script, starting with the Shebang line, which tells the kernel which binary program shall run the script.

Unfortunately it did not produce any output, ogc just started waiting for user input in its particularly silly way. What was wrong? I had to find out more about how interpreter scripts and shebangs work in Unix. It turned out the kernel passes the filename of the interpreter scipt as an argument to the interpreter. Upon executing Test, the kernel actually does this:

/usr/bin/ogc Test

Should have thought. A simple stdin-stdout filter is not enough, the interpreter itself shall be able to redirect its input into a specified file.

I modified ogc, so 3.0 can be used in both ways:

ogc Test
./Test

And to make it even more elegant, ogc 3.1 skips printing an output for whitespace/comment-only lines, including the shebang. The test script now does only what it really should: stressing 3 times what Life, Universe and Everything is all about.

Categories: Uncategorized Tags: ,