Register
It is currently Sat Oct 25, 2014 9:18 am

redirect an awk string output to a pipe


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Dec 28, 2009 8:10 am   

Joined: Mon Dec 28, 2009 8:01 am
Posts: 3
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


Top
 Profile  
 PostPosted: Mon Dec 28, 2009 8:16 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Quick fix, but I don't have time to figure out why it wouldn't work :P
Code:
./myscript.sh <param1> $(get_number EXAMPLE)


Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Dec 28, 2009 7:48 pm   

Joined: Mon Dec 28, 2009 8:01 am
Posts: 3
thanks, your line worked :).

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 :(


Top
 Profile  
 PostPosted: Tue Dec 29, 2009 4:54 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
a pipe is completely different from a command line option. A standard pipe like so;

Code:
command | othercommand


the othercommand can grab the piped data by opening STDIN and reading.
Command line options like so;

Code:
command param1 param2


are supplied to the command in an environment variable.

Also, assuming a modern version of bash, I note that although your way does work the safest approach is like so;

Code:
command param1 "$(othercommand paramx)"


I hope this help with your understanding of pipes :)


Top
 Profile  
 PostPosted: Tue Dec 29, 2009 7:26 pm   

Joined: Mon Dec 28, 2009 8:01 am
Posts: 3
I got it, thanks for the explanation. I was kind of confused but now I see that pipes read from stdin.

rid


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP