×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Include file

Include file

Include file

(OP)
Hi

I wish to create my little collection of awk 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 awk, gawk or mawk :
  • extension() function loads only from object files
  • specifying additional -f in the shebang does not work because arguments will be treatead as one
  • igawk can not be used in a shebang, because itself too is runed through a shebang
I tried the aboves with bash and tcsh.

Any idea ?

Thanks,
Feherke.
http://rootshell.be/~feherke/

RE: Include file

(OP)
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 main.awk : ---

CODE

#! /usr/bin/awk -f

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

{
  say(FILENAME,$0)
}

--- file unit.awk : ---

CODE

function say(a,b)
{
  print ">>> " a " -> " b " >>>"
}

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

Feherke.
http://rootshell.be/~feherke/

RE: Include file

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

RE: Include file

(OP)
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 main.awk ---

CODE

#! /usr/bin/awk -funit.awk

BEGIN {
  add(2,3)
}

--- file unit.awk ---

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.
http://rootshell.be/~feherke/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close