LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-10-2011, 12:32 AM   #1
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Rep: Reputation: 16
pipe output to argv


I've wrestled and googled. I'm still at a blank...

I want to run a program and pipe the output from
that program to something else. I'm confused as to what the reference is when the data comes out of the pipe. Here's an example:

# echo foo | echo $1
< nothing >

How can I get foo?

What I am trying to do is more like:

# expr 2 + 1 | mail -s"$1" me@example.com </dev/null

And me would get an email with "Subject: 3"



Thanks for any advice here.

-zeek
 
Old 06-10-2011, 12:55 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
mail -s $(expr 1 + 2) me@example.com </dev/null

Cheers,
Tink
 
Old 06-10-2011, 12:56 AM   #3
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
This works:

# echo foo | mail -s`xargs ` me@example.com </dev/null

To: me@example.com
Subject: foo

:-)


BUT can anyone tell me if there's some other reference like $1 or $something that references
what was piped?
 
Old 06-10-2011, 12:59 AM   #4
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
Thank you Tink! This would work well but for reasons specific to this script I *must* pipe the output to mail.

The xargs thing I posted just a second ago works... but still wondering what xargs is grabbing....

Quote:
Originally Posted by Tinkster View Post
Code:
mail -s $(expr 1 + 2) me@example.com </dev/null

Cheers,
Tink
 
Old 06-10-2011, 01:39 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
You seem to be confusing piping something, ie redirecting the output from one command to the standard input of the other
( this relies on the second command receiving its input in this way (echo does not)), with parameter arguments,
ie the values passed to a command or script on the command line ($1, $2, ...)

Tink's offering is more of what you require in this case.
 
Old 06-10-2011, 10:31 AM   #6
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
There's no confusion, just unsure what the parameter might be. xargs is a bit of hack but generally manages the piped output. As I noted earlier, this must be taken from a piped command.

Can anyone tell me what the reference is for output from a pipe?


Quote:
Originally Posted by grail View Post
You seem to be confusing piping something, ie redirecting the output from one command to the standard input of the other
( this relies on the second command receiving its nput in this way (echo does not)), with parameter arguments,
ie the values passed to a command or script on the command line ($1, $2, ...)

Tink's offering is more of what you require in this case.
 
Old 06-10-2011, 03:10 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by ezekieldas View Post
There's no confusion, just unsure what the parameter might be. xargs is a bit of hack but generally manages the piped output. As I noted earlier, this must be taken from a piped command.

Can anyone tell me what the reference is for output from a pipe?
There is no such thing. It's up to the receiving process to make sense
of what it gets. So in effect you'd need to write a wrapper that reads
the lines form the pipe via a read (with or without while loop), and then
handle the variables in whatever way you see fit.



Cheers,
Tink
 
Old 06-10-2011, 03:53 PM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 13.1
Posts: 1,328

Rep: Reputation: 254Reputation: 254Reputation: 254
You can start a subshell and therein use $0, $1,...:
Code:
echo 1 2 3 | xargs sh -c 'echo $2 $1 $0'
Note that they start with $0.
 
Old 06-11-2011, 05:07 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,515

Rep: Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891Reputation: 2891
Quote:
There's no confusion, just unsure what the parameter might be.
This statement actually proves my point. There is no parameter on the other side of a pipe. Your solution to use xargs is what
is generating parameters as per Reuti's example.
 
Old 06-11-2011, 10:04 AM   #10
ezekieldas
Member
 
Registered: Mar 2010
Posts: 122

Original Poster
Rep: Reputation: 16
That's fine if you need to have the last word here. We have a working solution now that's based on the initial presumption. I appreciate your comments.

Quote:
Originally Posted by grail View Post
This statement actually proves my point. There is no parameter on the other side of a pipe. Your solution to use xargs is what
is generating parameters as per Reuti's example.
 
Old 06-11-2011, 04:36 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by ezekieldas View Post
That's fine if you need to have the last word here. We have a working solution now that's based on the initial presumption. I appreciate your comments.
Ummm ... you're asking the same thing over and over, getting
technically correct answers, and then you complain about getting
them, making a snide remark about having to have the last word?

What goes into a pipe is not a list of parameters. The pipe as
such just passes on a stream of data. It's the receivers duty
to do with it as he sees fit; whether you (ab-)use xargs or do
so, or whether you run it through a read to chop the data up is
entirely up to you; there is no "parameters" falling out of the
pipe for you to peruse.



Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I pipe output to gtkdialog? Barbarax Programming 3 09-10-2011 09:04 PM
Pipe output of find through rm? CelticBlues Programming 3 07-29-2008 03:09 AM
Output truncated when using pipe tells Linux - General 1 12-16-2003 08:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration