redirect an awk string output to a script input with pipes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting redirect an awk string output to a script input with pipes
# 1  
Old 12-28-2009
redirect an awk string output to a script input with pipes

Hi,

I have a function in a bash script that returns a string after some operations using awk.

The following code returns 555
Code:
$VARIABLE="EXAMPLE"
get_number $VARIABLE

this value I'd like to pass it as a second argument of another script with the following usage
Code:
myscript.sh <param1> <param2>

I'm trying to pass the second argument using a pipe but it does not work, no idea why not
Code:
get_number $VARIABLE | myscript.sh param1
myscript.sh error: second argument is missing.

Can anyone help me?
many thanks,
rid
# 2  
Old 12-28-2009
try this :

Code:
get_number $VARIABLE | xargs -I{} myscript.sh param1 {}


Last edited by pludi; 12-28-2009 at 10:30 AM.. Reason: added code tags
# 3  
Old 12-28-2009
myscript.sh param1 "`get_number $VARIABLE`"
# 4  
Old 12-28-2009
Thanks both.

This line worked
Quote:
Originally Posted by alister
myscript.sh param1 "`get_number $VARIABLE`"

For info, the following one also works

Code:
myscript.sh <param1> $(get_number $VARIABLE)

Still don't understand why it does not work with the pipe Smilie
# 5  
Old 12-28-2009
You need to have a command in your script that can receive the pipe, like read.
I concocted this:
Code:
if ! [ -t 0 ]; then  # if stdin is not a terminal
  read params
  set -- $params
fi
echo $1
echo $2

Code:
 ./test  555 123
555
123

Code:
$ echo 555 123 | ./test
555
123

In this example the pipe overrides the command line parameters, but you can change that of course..
# 6  
Old 12-28-2009
Quote:
Originally Posted by rid
Still don't understand why it does not work with the pipe Smilie
It seems you are conflating pipes and command line options. They are unrelated concepts.

A pipe redirects the standard output of one command into the standard input of another. To read what is being sent in via a pipe, a process has to read from standard input using file i/o.

Command line options are usually made available to a program/script through an array (argv for C executables) or special variables ($*, $@, $1, $2, etc) for shell scripts. getopt (a function in the C library and a command for shell script use) is used to process the command line options.

These are two different mechanisms for passing information. If you want to pass what is in param2 in via a pipe, myscript.sh would have to read it from its stdin. However, it's probably using the variable $2 as it expects it on the command line.

Hopefully that helps clear it up. If not, perhaps it's a start.

Take care,
alister
# 7  
Old 12-29-2009
I got it, thanks for the explanation. I was kind of confused but now I see that pipes read directly from stdin.

rid
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Redirect output to the same input file in awk

Hi, I want to compare a value from test file and redirect the o/p value to the same file input file 250 32000 32 128 Below is my code awk '{ if ($1 < "300") print $1 > /tmp/test}' test want to compare 250 < 300 then print 300 to the same place below is the... (24 Replies)
Discussion started by: stew
24 Replies

2. Shell Programming and Scripting

Redirect the output of the file based on String

Hi, I need to redirect the output of the file until the first instance of a string is found. So in the filename output.txt i was to redirect everything from the start to where i find this string "BEA-000377" is found to a new file called output_new.txt Sample output.txt the controller... (1 Reply)
Discussion started by: mohtashims
1 Replies

3. Shell Programming and Scripting

Redirect script output to a file and mail the output

Hi Guys, I want to redirect the output of 3 scripts to a file and then mail the output of those three scripts. I used below but it is not working: OFILE=/home/home1/report1 echo "report1 details" > $OFILE =/home/home1/1.sh > $OFILE echo... (7 Replies)
Discussion started by: Vivekit82
7 Replies

4. Shell Programming and Scripting

How to redirect the output of awk to different variables?

Hi, I search for a string "can be any" and print the fields $4,$6 in to variables. $ cat input <value ='can be any' string='many' version='123'/> <value ='xyz' string='less' version='456'/> $ cat tried_code SRG=`awk -F\' '{ print $4 }' input` VER=`awk -F\' '{ print $6 }' input` ... (4 Replies)
Discussion started by: greet_sed
4 Replies

5. Shell Programming and Scripting

script to mail monitoring output if required or redirect output to log file

Below script perfectly works, giving below mail output. BUT, I want to make the script mail only if there are any D-Defined/T-Transition/B-Broken State WPARs and also to copy the output generated during monitoring to a temporary log file, which gets cleaned up every week. Need suggestions. ... (4 Replies)
Discussion started by: aix_admin_007
4 Replies

6. UNIX for Advanced & Expert Users

Complex Input/Output Redirect

Hi All, Sorry if the title is not good but I did not know how to explain with only some words! What I meant is: I have a unix command built from a private application vendor that when executed it prompts for two entries by the keyboard, let's say, for example: ... (1 Reply)
Discussion started by: felipe.vinturin
1 Replies

7. Programming

Redirect input and output to a shell script?

Dear All: I am trying to do something that (I thought) was relatively straightforward, but my code snippet does not seem to work. Any suggestions? Thank you Sincerely yours Misha Koshelev #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include... (0 Replies)
Discussion started by: misha680
0 Replies

8. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

9. Shell Programming and Scripting

to search a string and redirect according to output

i have a file (kk.log) in which following contents are there: ===================================== Enter:YOU HAVE ENTERED : a TestCase: TC1226677371 IS PASSED 2009-02-25 13:57:33.444 INFO com.t.t.exporterporter - Initialising Xml Exporter.... 2009-02-25 13:57:33.445 INFO ... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

10. Shell Programming and Scripting

Input file redirect in output path and want name as inputfilename_new.txt

not required this time (6 Replies)
Discussion started by: Sandeep_Malik
6 Replies
Login or Register to Ask a Question