I have an AWK script which looks like
#!/usr/bin/gawk -f
BEGIN { print myVar; }
{ print; }
which I can run locally in a Unix shell via the command
./myScript.awk -v myVar=value /tmp/inputfile
It correctly prints “value” and then dumps the inputfile.
I set up an apache web server with CGI enabled and created the following myScript.cgi:
#!/usr/bin/gawk -f
BEGIN {
print "Content-type: text/plain";
print "";
print myVar;
}
{ print; }
I can run it by pointing my web browser to
http://localhost/cgi-bin/myScript.cgi?/tmp/inputfile
but how can I pass the “-v myVar=value” bit through the URL?
You cannot pass a ‘-v’ variable through the CGI interface. The only way to access information is via a number number of environment variables that the CGI interface sets for you. You can access these in awk through the
ENVIRONarray. You are probably mostly interested in theQUERY_STRINGvariable, so try this:When accessed as
http://localhost/cgi-bin/myScript.cgi?myVar=foo&otherVar=bar,ENVIRON["QUERY_STRING"]will containmyVar=foo&otherVar=bar. You will have to process this string yourself to extract your variables. For example (untested):