[Linux-disciples] xargs questions

Stephen R Laniel steve at laniels.org
Wed Nov 10 21:32:13 EST 2004


I only recently caught onto xargs. Some questions:

1) I know there are more efficient ways to do this, but I
used apt-show-versions to find me every upgradeable gnome
package, using

asv '^gnome' |ig upgradeable

(asv = 'apt-show-versions -r', ig = 'grep -i')

then extracted the name of each upgradeable package from
each line and fed it through xargs to upgrade it:

asv '^gnome' |ig upgradeable |sed 's/^\(.*\)\/.*$/\1/' |xargs -i sagit '{}' 

where sagit = 'sudo apt-get install -t unstable'. When I run
this command line, apt-get install processes the lines one
at a time, which the man page implies is the inevitable
result of using -i (see xargs man-page excerpt below). If I
drop the -i, I get an error that the package '{}' doesn't
exist.

If instead I take the output of sed and pipe it through
"tr '\n' ' '" -- to convert all the newlines to spaces --
xargs seems to send along a final newline that it shouldn't
send, which ends up answering 'no' to a question that
apt-get install asks me. This aborts the installation.

So: how do I get all the lines output by sed to be processed
through xargs the way I expect?

2) I see in the Advanced Bash-Scripting Guide [1] that xargs
is used as follows:

tail -$LINES /var/log/messages | xargs |  fmt -s >>logfile

What is the point of using xargs here? All it seems to do
is remove trailing newlines from each line. That doesn't
seem so useful, so I must be missing the point.

I thought the point of xargs was to create commands that
fit on the command line -- e.g., if there were too many
upgradeable packages in my problem above, bash would tell
me that my command line was too long and abort. xargs is
then essentially a hack to get around a deficiency in bash,
right? Or no? If that's its job, what is it doing in the
'tail -$LINES' command line above?

I appreciate any help. I can't find any good documentation
for xargs, even in /usr/share/doc/findutils.

[1] - http://shorl.com/husustyfufefry

       --replace[=replace-str], -i[replace-str]
	      Replace occurences of replace-str in the initial arguments  with
	      names  read  from  standard input.  Also, unquoted blanks do not
	      terminate arguments.  If replace-str is omitted, it defaults  to
	      "{}" (like for `find -exec').  Implies -x and -l 1.

       --max-lines[=max-lines], -l[max-lines]
	      Use  at  most  max-lines	nonblank input lines per command line;
	      max-lines defaults to 1 if omitted.  Trailing  blanks  cause  an
	      input  line  to  be  logically continued on the next input line.
	      Implies -x.


-- 
``Here, at last, are the cats.''
 -Cosma Shalizi, http://shorl.com/bastojepikyfro



More information about the Linux-disciples mailing list