How to pass values between awk and shell scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass values between awk and shell scripts
# 1  
Old 10-30-2006
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  
Old 10-30-2006
# 3  
Old 10-30-2006
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  
Old 03-19-2009
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  
Old 03-19-2009
As described in this thread....
Code:
system(cmd) | getline val
close(cmd)

# 6  
Old 03-19-2009
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  
Old 03-19-2009
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass values to case statement in a function korn shell

I'm in the process of writng a function that consists of a case statement is there a way of calling the function and passing a value to it? ie function1 () { case opt1 do ..... opt2 do..... esac } function opt1 I'm aware the syntax is not correct, but you get the general idea. (1 Reply)
Discussion started by: squrcles
1 Replies

2. Shell Programming and Scripting

Pass values from web form to shell script

Hi, is it possible to pass more values from web form like textbox to shell script and if yes,how to do that.:confused::confused::confused: (2 Replies)
Discussion started by: tdev457
2 Replies

3. UNIX for Dummies Questions & Answers

How to pass a variable from shell to awk

I know this topic has been dealt with previously, but the solutions I've seen don't work for me apparently. I need to pass a variable defined in the shell to one in awk: $ echo $var1 3 $ cat aaa aaa 1 bbb 2 ccc 3 ddd 4 eee 5I've tried this, without success: $ awk... (2 Replies)
Discussion started by: metaltree
2 Replies

4. Shell Programming and Scripting

How to Pass the Output Values from the PL/SQL Procedure to Shell Script?

hi, Could anyone tell me how to pass the output values of the PL/SQL procedure to Shell script and how to store that values in a shell script variable... Thanks in advance... (5 Replies)
Discussion started by: funonnet
5 Replies

5. Shell Programming and Scripting

Call java program from shell and pass values

Hi All, Can anybody please help me with how can i call my java program from shell and also pass parameter along with it so that the program can interpret the value/int and update the database. Thanks in advance Neha (1 Reply)
Discussion started by: Neha Goyal
1 Replies

6. Shell Programming and Scripting

how to pass the values to unix shell from the oracle stored procedure.

Hi i am calling a stored procedure from unix shell like this call test_proc('0002','20100218'); the stored procedure was giving output like this dbms_output.put_line(' processed earlier'); i want to see the output in the unix shell where i called. Thanks barani (6 Replies)
Discussion started by: barani75
6 Replies

7. Shell Programming and Scripting

How to pass pl/sql table values to shell script

Hello, i am using '#!/bin/bash', i want to make a loop in pl/sql, this loop takes values from a table according to some conditions, each time the loop choose 3 different variables. What i am not able to do is that during the loop i want my shell script to read this 3 variables and run a shell... (1 Reply)
Discussion started by: rosalinda
1 Replies

8. Shell Programming and Scripting

need help on shell script(to pass the values)

only the arguments that are written to the file, my script is (sh /u01app/wkf.sh"$start_no","$name","$Condition","$file_name") like that when ever I run my script I need to write into a new file every time, like wise I have upto10 files with different names.bec my $start_no and $name will... (1 Reply)
Discussion started by: sai123
1 Replies

9. UNIX for Dummies Questions & Answers

How to pass values from one file to different scripts

Hi Gurus, Iam new to Unix. I had a requirement where i should use a file for example Host.sh or Host.txt Host = 555.254.45.14 username = aaaa pwd = SSSSSS123 And this file need to be used in different scripts. For example i have script1.sh where i want to use the values of... (6 Replies)
Discussion started by: pssandeep
6 Replies

10. UNIX for Dummies Questions & Answers

how to pass values from oracle sql plus to unix shell script

how to pass values from oracle sql plus to unix shell script (2 Replies)
Discussion started by: trichyselva
2 Replies
Login or Register to Ask a Question