Using cmd-line pipe with and without xargs

Using cmd-line pipe with and without xargs

Post by kf4.. » Wed, 28 Jun 2006 01:36:10



I was beating my head over this one for a few days.

I've two simple C programs.  The first futzes about and finally
fprintf's to stdout.  My second program was to accept the first's
output as its (the second) argv[1]:

./prog1 | ./prog2

result was:  "Usage: ./prog1 {string}" which tells me that the second
prog's 'if' caught that fact that argc != 2.  Fine, but why??

I did my two days of head bashing and (re-)discovered 'xargs' which
I've used before and didn't think to try out for whatever reason.

./prog1 | xargs ./prog2

Output was as I expected from the attempt w/o xargs...  Again, why??

My question, ultimately then, is what's the difference between attempt
one without xargs and the attempt with.  I read xargs man page and it
makes perfect sense and works as I've been accustomed to it working.

K&R's "The C Programming Language, 2ed.", pg 152: "If pipes are
supported, 'prog | anotherprog' puts the standard output of prog into
the standard input of anotherprog."  (OS in this case, FWIW, is Linux
2.6.9-11 with bash-3.00.15 for shell)

What did I misinterpret in K&R's writeup?  Pointers to a doc welcome,
and thanks a bunch for helping me clear this up.

73 de KF4PTH
Sean

 
 
 

Using cmd-line pipe with and without xargs

Post by Jon LaBadi » Wed, 28 Jun 2006 02:12:51



> I was beating my head over this one for a few days.

> I've two simple C programs.  The first futzes about and finally
> fprintf's to stdout.  My second program was to accept the first's
> output as its (the second) argv[1]:

> ./prog1 | ./prog2

> result was:  "Usage: ./prog1 {string}" which tells me that the second
> prog's 'if' caught that fact that argc != 2.  Fine, but why??

> I did my two days of head bashing and (re-)discovered 'xargs' which
> I've used before and didn't think to try out for whatever reason.

> ./prog1 | xargs ./prog2

> Output was as I expected from the attempt w/o xargs...  Again, why??

> My question, ultimately then, is what's the difference between attempt
> one without xargs and the attempt with.  I read xargs man page and it
> makes perfect sense and works as I've been accustomed to it working.

> K&R's "The C Programming Language, 2ed.", pg 152: "If pipes are
> supported, 'prog | anotherprog' puts the standard output of prog into
> the standard input of anotherprog."  (OS in this case, FWIW, is Linux
> 2.6.9-11 with bash-3.00.15 for shell)

> What did I misinterpret in K&R's writeup?

Command line arguments are not a form of "input".
They are data supplied to the program at its startup.
You can supply a constant value to a variable in your code, eg. x = 3.
The OS supplies the command line arguments in an analogous way.

Conversely, "input" is read by the application (from a file etc.)
after start up.  It is not "read" from the command line.

You might have done things like

    prog1 > /tmp/foo
    prog2 `cat /tmp/foo`     # or prog2 $(cat /tmp/foo)

or

    prog2 `prog1`            # prog2 $(prog1)

Of course metacharacters in the output of prog1 have to be considered.

 
 
 

Using cmd-line pipe with and without xargs

Post by kf4.. » Wed, 28 Jun 2006 02:30:49


[snip]

Quote:> > K&R's "The C Programming Language, 2ed.", pg 152: "If pipes are
> > supported, 'prog | anotherprog' puts the standard output of prog into
> > the standard input of anotherprog."  (OS in this case, FWIW, is Linux
> > 2.6.9-11 with bash-3.00.15 for shell)

[snip]

Quote:> Command line arguments are not a form of "input".
> They are data supplied to the program at its startup.
> You can supply a constant value to a variable in your code, eg. x = 3.
> The OS supplies the command line arguments in an analogous way.

> Conversely, "input" is read by the application (from a file etc.)
> after start up.  It is not "read" from the command line.

> You might have done things like

>     prog1 > /tmp/foo
>     prog2 `cat /tmp/foo`     # or prog2 $(cat /tmp/foo)

> or

>     prog2 `prog1`            # prog2 $(prog1)

> Of course metacharacters in the output of prog1 have to be considered.

DOH!

If I'm following the bus, what I did was neglect to read stdin from my
prog2.  What it sounds like from your explanation is cmd line args are
essentially variables assigned at run time to **argv and -not- from
stdin.  I guess I've confused what I would usually type on the command
line as the stdin to a program and, by doing so, assumed that output
from prog1's stdout would easily pipe as command line args to prog2.

stdin != **varg

[kicking myself in the rear]

Thanks, Jon!

 
 
 

1. line by line without using echo "$i" | while read line?

i=$(ps -ef)
and
I do not want to use
echo "$i" | while read line
any options?

The reason I do not want to use "| while"
is that it will run a subshell:

pipe()                                          = 3 [4]
fork()                                          = 1089
    Received signal #18, SIGCLD [caught]
      siginfo: SIGCLD CLD_EXITED pid=1089 status=0x0000

And the environment variable defined in the while loop
can not get out of the loop in Borne shell.

I tried

IFS="\n"
for j in $i

but it does not work.

Thanks.

2. Sorry: I had to try it!

3. piping lines and building argument lists for commands; xargs ??

4. PROBLEM: USB Serial oops in 2.4.22-pre2

5. Any cmd line tricks with named pipes?

6. FTP-Upload change rights?

7. sun raster to gif from cmd-line

8. NIS and autofs with distributed /home

9. sh cmd-line arg splitting

10. is there a way to have var. assignments on cmd line affect only a builtin cmd?

11. disk space cmd for cmd line entries

12. Q: making LILO cmd-line args permanent

13. self defined cmd-line params for awk in AIX