Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Include file 1

Status
Not open for further replies.

feherke

Programmer
Aug 5, 2002
9,540
RO
Hi

I wish to create my little collection of [tt]awk[/tt] functions and use them in complex scripts. And to run the complex script as an executable, not specified as an argument.

Sadly, after a lot of googling and man reading I not found any solution for [tt]awk[/tt], [tt]gawk[/tt] or [tt]mawk[/tt] :
[ul]
[li][tt]extension()[/tt] function loads only from object files[/li]
[li]specifying additional [tt]-f[/tt] in the shebang does not work because arguments will be treatead as one[/li]
[li][tt]igawk[/tt] can not be used in a shebang, because itself too is runed through a shebang[/li]
[/ul]

I tried the aboves with [tt]bash[/tt] and [tt]tcsh[/tt].

Any idea ?

Thanks,
Feherke.
 
Hi

Although the amount of answers does not indicate a high rate of interest, I would like to mention the solution finally used. Just for the archive, a simple example.

--- file [tt]main.awk[/tt] : ---
Code:
#! /usr/bin/awk -f

BEGIN {
  if ([red]!stop[/red]) {
    for (i=1;i<ARGC;i++) argv=argv " " ARGV[i]
    system("/usr/bin/awk -v [red]stop=1[/red] -f unit.awk -f main.awk" argv)
    exit
  }
}

{
  say(FILENAME,$0)
}

--- file [tt]unit.awk[/tt] : ---
Code:
function say(a,b)
{
  print ">>> " a " -> " b " >>>"
}

Sadly, this method is far to be perfect, but for my current problem is efficient.

Feherke.
 
Hey Guys,

I know this was posted a while ago, but it was something i was scratching my head with for the past day or two, so it was a relief to find that i wasn't the only one.

I don't know how much you might have evolved this, but i've quickly built up the basic part of this script into functions to give it a similar functionality to python, perl etc,


Code:
#!/usr/bin/awk -f
function include(_includeFile_) {INCLUDE_FILES[_includeFile_]}
function sourceIncludes() {
	if (!load) {
		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 load=1 -- %s\n",scriptname,inc,args)
		system(cmd) ; exit
	}
	
}

BEGIN {
	#include("coreTools.awk")
	include("printme.awk")
	sourceIncludes()
	
	printThis("oy")

}

printme.awk lives inside the AWKPATH variable,
Code:
function printThis(printThisSTr) {
	print "ello " printThisSTr
}

again it's far from elegant, and it will fall over if it can't find the file, could easily enough add a check to see if the file exists before allowing it into the INCLUDE_FILE array, but this will do for me .


I'm suprised that there isn't an easier way. I kinda expected this functionality to be built in. Has anybody else had fun with this as i would be intrested to know if there is a more straight forward solution.

anyway cheers for the work around feherke.

arodef
 
Hi

Nice work, arodef. I appreciate its flexibility and the possibility to include more then one file. But frankly, I prefer less code to write for such a common operation.

Another way I started to use meantime is abit different : works only with one include file, but the code for inclusion is in the unit.

--- file [tt]main.awk[/tt] ---
Code:
#! /usr/bin/awk [red]-funit.awk[/red]

BEGIN {
  add(2,3)
}

--- file [tt]unit.awk[/tt] ---
Code:
#! /usr/bin/awk -f

BEGIN {
  if (!stop) {
    for (i=1;i<ARGC;i++) args=args " -f " ARGV[i]
    system("/usr/bin/awk -v stop=1 -f unit.awk" args)
    exit
  }
}

function add(a,b)
{
  print a " + " b " = " a+b
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top