awk getline

Hi,

I have an awk script with the following function in it .

 function cmd( c )
        {
        
        
                while( ( c | getline foo) > 0 ){
                         return foo ;
                          close( c );

                         }

        }
c = date "+%d" -d "3/6/17 16:26:35 GMT-03:30";

I'm basically trying to convert a given time to system localtime and return back the value to awk.
The script works fine, but if I call the function multiples times I get no return from the function. I'm not sure why, any tip will be appreciated.

Thanks

well your close(c) is unreachable and you hit return foo FIRST. And you probably run out of max open file descriptors of awk - depending your version of awk it might be 5 (for AIX, if I recall).

I'd change this to:

function cmd(c,   foo )
{
  while( ( c | getline foo) > 0 ){
    close( c )
    return foo
  }
}

In addition to the point vgersh99 makes, since the function will only return one line anyway, and the foo variable is now a local variable which gets initialized as empty, you might as well skip while loop and test :

function cmd(c,   foo){
  c | getline foo
  close( c )
  return foo
}

For multi-line output the while loop would not function anyway.

Hi Scrutinizer,
The point of the loop in this code seems to be to return data from the last line of text produced by the command named by c . Without the loop, the function returns the first line of text produced instead of the last. Neither form gathers more than one line of output.

Thanks for the tips.

I got it to work as follow

function cmd(c,   foo){
 close( c )
system("") 

(c | getline foo)    
  return foo 
}