Environment Variables in NewtonScript and NEWT/0 ¬
2012-01-22
To expand upon my previous post demonstrating how to access command line arguments (ARGV) in NewtonScript in NEWT/0, I’ll demonstrate how to access environment variables. Command line arguments are only the beginning for writing command line tools as you often need to access environment variables as well. In some situations, like running a script as a CGI in your favorite HTTP server software, environment variables are really your only way to receive input from the outside world.
After digging through the NEWT/0 source code again, it appeared that I could again use GetGlobalVar()
, but this time with the '_ENV_
object literal, but it didn’t seem to have anything useful in it, certainly not any environment variables. So, back to the source code I went and discovered that NEWT/0 implements a custom global function GetEnv()
, like the getenv()
equivalent in C, accepts a string for the environment variable name and returns the value stored in said environment variable.
To get the PATH
environment variable, for example:
#!/usr/bin/env newt
Print("PATH:" && GetEnv("PATH") && "\n");
Or a basic “hello world” CGI that also echoes the query string:
#!/usr/local/bin/newt
Print("Content-Type: text/plain\n\n");
Print("Hello world!\n\n"); Print("QUERY_STRING:" && GetEnv("QUERY_STRING") && "\n");
I must thank NEWT/0’s developer, Makoto Nukui, for including all this functionality!