Shebang
Shebang is a character sequence (#!), used in Unix-based operating systems, that enables on a text file to indicate a program interpreter to execute the rest of the remaining of the file.
OpenAF also supports this unix functionality and makes it easy to transform any existing OpenAF script into a shebang unix script.
Simple Unix example
If you already know how Unix’s Shebang functionality works you can skip to the next section. If not let’s start with a simple Unix shell script editing a sample file example.sh:
NAME=Scott
echo Hello $NAME
If you execute it will print the expected result:
$ /bin/sh example.sh
Hello Scott
To avoid having to mention the script interpreter (e.g. /bin/sh) in Unix you can add a “shebang” editing the header of the example.sh file:
#!/usr/bin/sh
NAME=$1
echo Hello $NAME
Then you just need to set execution permissions and execute it:
$ chmod +x example.sh
$ ./example.sh Tom
Hello Tom
Setting shebang for OpenAF
Let’s start by a very simple OpenAF script example.js:
var name = "Scott";
print("Hello " + name);
Executing with OpenAF as you would normally do:
$ [openaf path]/oaf -f example.js
Hello Scott
Adding shebang
To add the shebang and set the execution permissions in Unix just execute with OpenAF:
$ [openaf path]/oaf --sb example.js
That’s it! It’s ready, just execute directly:
$ ./example.js
Hello Scott
Using parameters
After using OpenAF to set shebang on a existing script it actually pre-appends the necessary code to use command-line parameters. Let’s go through the changes necessary to provide a custom name:
$ ./example.js name=Tom
Let’s start by looking at the generated code in example.js:
#!/usr/bin/env /openaf/oaf-sb
var params = processExpr(" ");
// sprint(params);
var name = "Scott";
print("Hello " + name);
Apart from the first shebang line there are two extra lines added by OpenAF:
var params = processExpr(" ");
// sprint(params);
The first one creates a params variable to capture the result of calling the OpenAF function processExpr. This OpenAF function takes one argument (e.g. “ “) as the command-line separator between command-line parameters and will return a javascript map with the corresponding result. Let’s try out some examples to better understand how it works. To test it just temporially uncomment the sprint line added previously by OpenAF:
#!/usr/bin/env /openaf/oaf-sb
var params = processExpr(" ");
sprint(params);
var name = "Scott";
print("Hello " + name);
Now try executing with different parameters combinations (note: openaf expects parameters to be a pair of key and corresponding value separated with ’=’):
$ ./example.js name=Tom
{
"name": "Tom"
}
Hello Scott
$
$ ./example.js name=Tom something other=stuff
{
"name": "Tom",
"something": "",
"other": "stuff"
}
Returning to the initial propose we would like to use the name parameter to customize the name to display. So let’s assign it to the existing name variable:
#!/usr/bin/env /openaf/oaf-sb
var params = processExpr(" ");
// sprint(params);
var name = params.name; // <-- assigning the name parameter
print("Hello " + name);
Executing it after the last changes:
$ ./example.js name=Tom
Hello Tom