Why does the gawk-function close return -1 ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why does the gawk-function close return -1 ?
# 8  
Old 04-15-2016
Quote:
Originally Posted by wilmaed
hallo,

My example1 is from this nawk-site:

www . staff.science.uu.nl/~oostr102/docs/nawk/nawk_39.html
This is fine, because it closes the *writer*. You are closing the *reader*.
# 9  
Old 04-15-2016
Sorry, No. You have to close any file (pipe/redirection/command), be it target for a print or source of a getline, in order to avoid exceeding awk's open file limit (1021 for my nawk, I guess OPEN_MAX - stdin, stdout, stderr).
# 10  
Old 04-15-2016
Yes, but only the writing end of the pipe, because if you close it, the reading end will close it by itself. At least this is what the example in th gawk docs say.

If you want to explicitly close both ends, you would need two close statements, but I guess the second one (for the right side of the pipe) will fail.
# 11  
Old 04-15-2016
Quote:
Originally Posted by rovf
Yes, but only the writing end of the pipe, because if you close it, the reading end will close it by itself. At least this is what the example in th gawk docs say.

If you want to explicitly close both ends, you would need two close statements, but I guess the second one (for the right side of the pipe) will fail.
No. An awk pipeline does not work that way.

Suppose that we have a file named 123 that contains:
Code:
1
2
3

With that file, and an executable shell script named tester containing:
Code:
#!/bin/ksh
awk '
BEGIN {	cmd = "cat 123"
	for(i = 1; i <= 4; i++) {
		if((rc = (cmd | getline)) == 1) {
			print $0 > ("no_input_close" i)
			close("no_input_close" i)
		} else if(rc == 0)
			print "EOF in 1st loop on read " i
		else	print "ERROR in 1st loop on read " i
	}
	close(cmd)
	for(i = 1; i <= 4; i++) {
		if((rc = (cmd | getline)) == 1) {
			close(cmd)
			print $0 > ("with_input_close" i)
			close("with_input_close" i)
		} else if(rc == 0)
			print "EOF in 2nd loop on read " i
		else	print "ERROR in 2nd loop on read " i
	}
}'

Running the script produces the output:
Code:
EOF in 1st loop on read 4

and the directory contents are:
Code:
-rw-r--r--  1 dwc  staff    6 Apr 15 14:20 123
-rw-r--r--  1 dwc  staff    2 Apr 15 14:48 no_input_close1
-rw-r--r--  1 dwc  staff    2 Apr 15 14:48 no_input_close2
-rw-r--r--  1 dwc  staff    2 Apr 15 14:48 no_input_close3
-rwxr-xr-x  1 dwc  staff  540 Apr 15 14:47 tester
-rw-r--r--  1 dwc  staff    2 Apr 15 14:48 with_input_close1
-rw-r--r--  1 dwc  staff    2 Apr 15 14:48 with_input_close2
-rw-r--r--  1 dwc  staff    2 Apr 15 14:48 with_input_close3
-rw-r--r--  1 dwc  staff    2 Apr 15 14:48 with_input_close4

and the contents of the files produced by the awk program are:
Code:
$ for i in *input*
> do      echo "*** $i"
>         cat $i
> done
*** no_input_close1
1
*** no_input_close2
2
*** no_input_close3
3
*** with_input_close1
1
*** with_input_close2
1
*** with_input_close3
1
*** with_input_close4
1
$

If the input file had been closed (as you suggested) when the output files ar closed in the 1st awk for loop, there would be four no_input_closeX instead of three and their contents would be the same as the with_input_closeX files. But, instead we see that even though the output files are closed in the 1st loop, the input command pipeline remains open and each getline in the 1st for loop gets a subsequent input line from the cat command instead of restarting at the 1st line as n the 2nd for loop where both the input and output streams are closed each time through the loop.

The only files that awk automatically closes are input files named as operands that are closed when EOF is reached.
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

3. Shell Programming and Scripting

This function (decode64) runs on gawk but not on busybox awk

Hello, I'm trying to figure out a way to use a decode64 function in an embedded system who has few utilities, including busybox. Right now have something like this (taken from "google base64-and-base85-encoding-awk-scripts" sorry, I'm not able to post urls yet) _decode64() { &&... (4 Replies)
Discussion started by: chilicuil
4 Replies

4. Shell Programming and Scripting

getting return value from oracle function

So, in Oracle (11gR2) on aix I have a function like: CREATE OR REPLACE function COMMON.t2(var1 in varchar2, vari in number,var2 in out number) return number as begin insert into korisnik_temp(kor_inicia, kor_opisno) values(1, var1); var2:=0; return var2; exception when... (12 Replies)
Discussion started by: bongo
12 Replies

5. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

6. Shell Programming and Scripting

return in function

I am using ksh. I want to know how can we make any function to return string or double value. I dont want to use the global variables. (5 Replies)
Discussion started by: PRKS
5 Replies

7. Shell Programming and Scripting

return value of a function

I have write a shell function to get the maximum of a vector. However, the returned value from the function is not always the correct one. Here is the script: maxval() { local max j i size arrval size=$1 ; shift max=-999999999 i=0 while do arrval="$1" if then ... (5 Replies)
Discussion started by: fl0r10
5 Replies

8. Shell Programming and Scripting

need to return value from function

how to return value from function and print from main program??? And also I need to return true or false... Is it possible? (5 Replies)
Discussion started by: darshakraut
5 Replies

9. Shell Programming and Scripting

return value of a function

Hi I have a doubt in the way the variables inside a function are treated . if a function is called from the main script directly, the variables inside them act as global variables. however if the return value of the function is stored to some other variable in the main script as shown,... (3 Replies)
Discussion started by: prez
3 Replies
Login or Register to Ask a Question