Sponsored Content
Full Discussion: external function in awk
Top Forums Shell Programming and Scripting external function in awk Post 302157224 by drl on Thursday 10th of January 2008 12:07:38 PM
Old 01-10-2008
Hi.

Here is a script that attempts various methods of calling functions:
Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate functions with awk.

#  ____
# /
# |   Infrastructure BEGIN

set -o nounset
echo

debug=":"
debug="echo"

## The shebang using "env" line is designed for portability. For
#  higher security, use:
#
#  #!/bin/sh -

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version bash awk

my_shell_local()
{
  echo " Local function $FUNCNAME called."
}

kamel_seg()
{
  echo " Function $FUNCNAME called with arguments \"$*\"."
}

echo
echo " Function is exported to sub-shells:"
export -f kamel_seg
typeset -F

# |   Infrastructure END
# \
#  ---

echo
echo " Calling shell function from shell:"
kamel_seg hello

echo
echo " Calling shell function from awk:"
awk '
BEGIN { print " In awk" ; kamel_seg }
'

echo
echo " Calling shell function from awk system() call:"
awk '
BEGIN { print "In awk - system" ;
 system(" echo Hi from system call" ) ; system( "kamel_seg from awk" ) }
'

echo
echo " Calling awk function:"
awk '
function my_function( message )
{
  print " awk function my_function called, argument:" message
}
BEGIN { print " Calling my_function from BEGIN block."
 my_function( " Hello there." )
}
'

echo
echo " Calling awk program that calls a shell script with system():"
awk '
BEGIN { print " Calling helper." ; system( "./helper" ) }
'

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
GNU bash 2.05b.0
GNU Awk 3.1.4

 Function is exported to sub-shells:
declare -fx kamel_seg
declare -f my_shell_local

 Calling shell function from shell:
 Function kamel_seg called with arguments "hello".

 Calling shell function from awk:
 In awk

 Calling shell function from awk system() call:
In awk - system
Hi from system call
 Function kamel_seg called with arguments "from awk".

 Calling awk function:
 Calling my_function from BEGIN block.
 awk function my_function called, argument: Hello there.

 Calling awk program that calls a shell script with system():
 Calling helper.
 Script "helper" called.
 awk script called from shell script helper

Among the other points, this illustrates that shell functions can be called from awk system(), provided the shell functions are exported (frankly, I was not expecting this; I simply was trying to exhaust all the possibilities).

The brief declare output shows the difference between local and exported functions.

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk help (external variable)

i need help with an awk question. i am looking to have an external variable be defined outside of awk but used in awk. so if we have fields $1, $2, $3 so on and so forth, i would like to be able to dictate what field is being printed by something like $. so if i had a counter called test, make it 3... (8 Replies)
Discussion started by: pupp
8 Replies

2. Shell Programming and Scripting

Calling external function in a shell

hi guys, how r u??? please I need you, help me please. I have a shell, in this shell i have this function and another code lines, this function is getting date one day back. the function is in the same shell (FILE 1) Now I need put this function in another file (FILE 2) and calling... (4 Replies)
Discussion started by: acevallo
4 Replies

3. Shell Programming and Scripting

Can you ref/link/import a function from external ksh script?

Hey guys, I'm not the best AIX scripter about but I can flounder my way thru them to create what I need. Anyhow, I have various scripts that perform various actions and processes. I was tasked to come up with a single form of logging that all the scripts could implement so that the output... (2 Replies)
Discussion started by: isawme
2 Replies

4. Shell Programming and Scripting

How to Call external function in .C or .So (Shared Object)

Hi, Anybody know any way to Call with Shell Script an external function wrote in .C or .So (Shared Object) on AIX enviroment and returning parameters of .C or .SO to Shell Script? Tks!! (6 Replies)
Discussion started by: rdgsantos
6 Replies

5. UNIX for Dummies Questions & Answers

Call external function

Is it possible to call a function from another script? Thanks (6 Replies)
Discussion started by: chrisjones
6 Replies

6. Shell Programming and Scripting

Help needed with awk external variable

I'm trying to get the universities result data into different file, where the $9 contains unversity field and field7,4 & 5 contains the keys to sort the students by marks. How to use uni variable to match against $9 inside awk? c=0 for uni in `cat /tmp/global_rank| awk -F ',' '{print... (1 Reply)
Discussion started by: InduInduIndu
1 Replies

7. Shell Programming and Scripting

Problem in passing date to external function from perl script.

my $sysdate = strftime('%Y-%m-%d', localtime ); biDeriveByDate('Table_Str',$sysdate,\@lIndx,\@lResVals) In a perl script, when I'm trying to pass $sysdate to some external function it's not working since $sysdate is passed as a string mentioned above but my function is expecting a date value... (1 Reply)
Discussion started by: Devesh5683
1 Replies

8. Shell Programming and Scripting

Passing external variable to awk

Hi, I am trying to write a bash script in which I need to pass a external variable to the awk program. I tired using -v but it not accepting the value. Here is my sample code. #!/usr/bin/bash ###################################################################################### ####... (5 Replies)
Discussion started by: jpkumar10
5 Replies

9. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

10. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 11:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy