I have an awk file that loads other awk files. Rather than calling the loading code everytime I run the main function of the file I'm trying to load everything in a BEGIN statement first, but if I do that the function itself never gets run. Is there anyway to have a BEGIN statement, and functions called from outside of the script?
My awk script:
#! /usr/bin/awk -f
function include(includeFile) {INCLUDE_FILES[includeFile]}
function sourceIncludes()
{
if(!l)
{
getline t < "/proc/self/cmdline"; split(t,T, "\0")
scriptname=T[3]
for (i = 1; i < ARGC; i++)
args=args " "ARGV[i]
for(iFile in INCLUDE_FILES )
inc = inc " -f "iFile
cmd=sprintf("%s %s -v l=1 -- %s\n",scriptname,inc,args)
system(cmd); exit
}
}
function pkginfo(pkg)
{
{ print pkg }
}
BEGIN {
include("wrap.awk")
sourceIncludes()
}
wrap.awk contents:
#! /usr/bin/awk -f
function wrap(text, q, y, z)
{
while(text)
{
q = match(text, / |$/)
y += q
if(y >= 80)
{
z = z RS sprintf("%c", 0x2502) #chr(2502)#"\\u2502"
for(i = 0; i < 20; i++)
z = z FS
y = q - 1
}
else if(z)
z = z FS
z = z substr(text, 1, q - 1)
text = substr(text, q + 1)
}
return z
}
This is how I call everything from bash / zsh:
awk -f ~/.ZSH_CUSTOM/awkscripts/pkginfo.awk -e '{ pkginfo("test") }'
wrap.awk?BEGINcommands started to run until it got to theexit; and then it processedexitbefore getting to the commands passed in with the-eswitch. Let me know if that's not what's going on for you.