Re: what should xargs do with empty stdin?

From: Todd Smith (tsmith_at_sgi.com)
Date: 01/03/04

  • Next message: Erik Max Francis: "Re: what should xargs do with empty stdin?"
    Date: 2 Jan 2004 23:44:01 GMT
    
    

    In article <3FF5E99C.BFD8D794@alcyone.com>, Erik Max Francis <max@alcyone.com> writes:
    |> Todd Smith wrote:
    |>
    |> > Say I write a sequence of commands to remove all C source files under
    |> > a particular root directory. If there happen not to be any such files
    |> > there, I get the following (unnecessary and annoying) error:
    |> >
    |> > > find . -name "*.[ch]" -print | xargs rm
    |> > Incorrect usage
    |> > Usage: rm [-fir] file ...
    |> ...
    |> > Is this the intended behavior? Is there a portable way of avoiding
    |> > this? On IRIX, I can use "xargs -L1", but the (GNU?) xargs on my
    |> > Linux system doesn't like -L or seem to have anything equivalent.
    |>
    |> Look at the error: It's _rm_ reporting this complaint, not xargs. Your
    |> question actually has nothing to do with xargs; it's how to get the
    |> command that xargs is going to run to accept no arguments. With rm, add
    |> the -f option.

    I understand that it is _rm_ reporting the complaint, but I think its
    complaint is justified.

    Certainly this behavior of xargs violates the principle of least
    surprise. If I ran the following in csh:

        foreach i (*.[ch])
            # loop body
        end

    in a directory with no C source files, I definitely wouldn't expect
    the loop body to still be executed once, with no value for $i, even if
    I could find some way (like adding a -f option to _rm_) to make it
    work.

    Some commands have different modes of operation, depending upon the
    presence or absence of arguments. Some uses of xargs specify a
    command *and* some initial arguments, to be followed by sets of
    arguments added by xargs. I would rather have xargs behave
    intuitively than have to go and ensure that each one of the
    (unlimited) set of command possibilities I might use with it is able
    to handle this boundary case gracefully.

    In any case, another poster pointed me to an appropriate option for
    GNU xargs, so at least I am able to get the behavior I wanted, if not
    in the same way for every platform's default xargs implementation.

    Todd Smith


  • Next message: Erik Max Francis: "Re: what should xargs do with empty stdin?"

    Relevant Pages

    • Re: Command line to remove duplicate files?
      ... Ohmster wrote: ... what xargs does? ... So what xargs does is pretty much pass on large numbers to the command ... Put "messageforohmster" in message body ...
      (alt.os.linux)
    • Re: Pipe output from find to mv
      ... I would suggest you to go with gnu xargs instead of -exec ... At first glance, the find command above was rather complicated, not to ... though- GNU find and everyone else's. ... we must use another command to sort such output-a ...
      (comp.unix.shell)
    • Re: Neophyte: ls with wildcards
      ... There's two ways to provide data for a command -- either as a command ... What xargs does is it reads data from standard input and then looks ... would lead to xargs eventually executing "ls /bin". ... to argument lists ...
      (comp.unix.shell)
    • How to use xargs with a shell redirect
      ... command with redirects in it? ... like a perfect opportunity for the shell (BASH shell, ... to do this with xargs... ...
      (comp.unix.shell)
    • Re: find -exec surprisingly slow
      ... deleting files from a directory which has 400K filenames in it, ... mv usermail usermail.bigspam ... The thing to use is the '-J' option of xargs. ... a single `mv' command as possible. ...
      (freebsd-questions)