The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how do I pass or read values from file ? nix-kid Shell Programming and Scripting 1 05-20-2008 01:04 PM
How to pass values from one file to different scripts pssandeep UNIX for Dummies Questions & Answers 6 12-14-2007 07:14 PM
How to pass passwords to bash scripts? siegfried Shell Programming and Scripting 5 08-04-2006 05:59 PM
how to pass values from oracle sql plus to unix shell script trichyselva UNIX for Dummies Questions & Answers 2 02-24-2006 10:19 AM
is it possible to pass external variable values to nawk? swamymns Shell Programming and Scripting 1 02-02-2006 06:13 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-30-2006
rajnikanth.1912 rajnikanth.1912 is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 5
How to pass values between awk and shell scripts

I know that we can call system command to execute shell script in awk.

but it does not return the result of the command executed , but only returns
the value of the command executoin status ( 1/0 --> failure / success).

Could anyone let me know how to solve this problem.
  #2 (permalink)  
Old 10-30-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
check this
http://www.unix.com/showthread.php?p...#post302094516
  #3 (permalink)  
Old 10-30-2006
kwachtler kwachtler is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 15
Below example will execute a shell command, and ...
1. Read cmd output into current line.
2. Read cmd output into awk variable.

1.
"shell cmd" | getline
2.
"shell cmd" | getline var

Good Luck !
KW
  #4 (permalink)  
Old 03-19-2009
islegmar islegmar is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 10
Question

Hi,

I have the same problem and I the solutions posted here don't work for me or I don't understand them :-( So, how can I retrive in awk the returned value by a shell command and not its return status (0,1)?

My problem: I want to perform some date manipulation inside awk. For this, I want to convert the dates in the number of seconds using the date shelll command.

cmd=sprintf("date -d %s +%%s",d1);
val=system(cmd);

The problem is that val receives 0 (success) and not the number of seconds.

Thanks a lot

Isi
  #5 (permalink)  
Old 03-19-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
As described in this thread....
Code:
system(cmd) | getline val
close(cmd)
  #6 (permalink)  
Old 03-19-2009
islegmar islegmar is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 10
Hi

Thanks for the reply but I get an error when using getline. Is getline an standard UNIX command (I'm using cygwin :-()? Can I use it inside an awk code?

My code is

Code:
awk -v inicio=$f1 -v fin=$f2 '
function compareDate(d1,d2)
{

  cmd=sprintf("date -d %s +%%s",d1);
  system(cmd)|getline s1;
  close(cmd);
  cmd=sprintf("date -d %s +%%s",d2);
  system(cmd)|getline s2;
  close(cmd);

  if (s1 > s2)
  {
    retVal=1;
  }
  else if ( s1=s2 )
  {
    retVal=0;
  }
  else
  {
    retVal=-1;
  }


  return retVal;
}

doprint==0 && compareDate($1,inicio)>=0 { doprint=1; }
doprint==1 && compareDate($1,fin)>=0    { exit; }
doprint==1 { print;  }
When I execute it, s1 and s2 are always empty and what I see in stdout is the ouput of the 'date' commands

Thanks a lot again

Isi
  #7 (permalink)  
Old 03-19-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
sprintf("date -d %s +%%s",d1)
This actually expects TWO parameters passed to it - you're just passing ONE (d1). Where's the second?
Do you get anything spilled to stderr complaining about 'sprintf'?
What's this '+%%s' supposed to be?
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0