[p4] p4 -x- flag with client -d?
David Weintraub
qazwart at gmail.com
Fri Mar 30 07:29:25 PST 2007
David Weintraub wrote:
> cat obsolete_clients_list.txt | xargs p4 client -d
>
> "xargs" will not work as you shown because "xargs" loads up as many
> parameters as it can on the command line, before running the command.
> On 3/29/07, Ken Williams <ken.williams at thomson.com> wrote:
> Not true, it will work.
> However, there are assumptions regarding the format of the
> obsolete_clients_list.txt. It works fine if for example the client names
> are on separate lines. I'm also assuming a recent Linux edition of
> xargs, as there may be other xargs versions or implementations that
> behave differently.
Here's my OS:
$ uname -a
Linux aladdin 2.6.5-7.97-smp #1 SMP Fri Jul 2 14:21:59 UTC 2004 x86_64
x86_64 x86_64 GNU/Linux
It's a fairly new version of Linux
I created a few clients: "bob", "carol", "ted", and "alice":
I then ran:
$ p4 client -d bob carol ted alice
Usage: client -d [ -f ] clientname
Missing/wrong number of arguments.
Which is what I expected. "p4 client" only takes one client name as an argument
I created a file called "obsolete_clients_list.txt" and the file
contains one client per line:
bob
carol
ted
alice
I then ran the following command:
$ xargs p4 client -d < obsolete_clients_list.txt
Usage: client -d [ -f ] clientname
Missing/wrong number of arguments.
Okay, let's try it this way to verify that the redirection didn't do anything:
$ cat obsolete_clients_list.txt | xargs p4 client -d
Usage: client -d [ -f ] clientname
Missing/wrong number of arguments.
Still doesn't work. Since this is the GNU version of xargs (which is
what Linux comes with), I can add the "--interactive" flag, and that
will show me the command before executing it:
$ xargs --interactive p4 client -d < obsolete_client_list.txt
p4 client -d bob ted carol alice ?...y
Usage: client -d [ -f ] clientname
Missing/wrong number of arguments.
As you can see, even though I set my file up for one client per line,
it put all the lines together, so it only has to execute "p4 client"
just once. That is the purpose of "xargs", to save processor time by
combining files on the command line, so the command only has to be
executed just a few times instead of once per file. It was really made
to work with the "find" command:
$ find . -name "*.temp" | wc -l
6423
$ find . -name "*.temp" -exec rm { } \;
As you can see, the find command will now call "rm" 6,423 times.
However, buy using the xargs command like this:
$ find . -name "*.temp" | xargs rm
It only has to run the "rm" command once, maybe twice.
Historically, xargs automatically combined file names on the command
line and if you needed to run the command on a once per file name
basis, you would write a quick "while read" loop (like my original
reply). However, in BSD and Linux implementations, a switch was added
to change this behavior. In BSD, it is "-n". In Linux, it is
"--max-args".
Since I am on a Linux system, this will work:
$ xargs --max-args=1 p4 client -d < obsolete_client_list.txt
Client bob deleted.
Client ted deleted.
Client carol deleted.
Client alice deleted.
On 3/30/07, Geir A. Myrestrand <geir.myrestrand at falconstor.com> wrote:
> David Weintraub wrote:
> > On 3/29/07, Ken Williams <ken.williams at thomson.com> wrote:
> >> cat obsolete_clients_list.txt | xargs p4 client -d
> >
> > "xargs" will not work as you shown because "xargs" loads up as many
> > parameters as it can on the command line, before running the command.
>
> Not true, it will work.
>
> However, there are assumptions regarding the format of the
> obsolete_clients_list.txt. It works fine if for example the client names
> are on separate lines. I'm also assuming a recent Linux edition of
> xargs, as there may be other xargs versions or implementations that
> behave differently.
Okay, a bit of an experiment:
Here's my OS:
$ uname -a
Linux aladdin 2.6.5-7.97-smp #1 SMP Fri Jul 2 14:21:59 UTC 2004 x86_64
x86_64 x86_64 GNU/Linux
It's a fairly new version of Linux
I created a few clients: "bob", "carol", "ted", and "alice":
I then ran:
$ p4 client -d bob carol ted alice
Usage: client -d [ -f ] clientname
Missing/wrong number of arguments.
Which is what I expected. "p4 client" only takes one client name as an
argument. No real issues there.
I created a file called "obsolete_clients_list.txt" and the file
contains one client per line:
bob
carol
ted
alice
I then ran the following command:
$ xargs p4 client -d < obsolete_clients_list.txt
Usage: client -d [ -f ] clientname
Missing/wrong number of arguments.
Okay, it removes the "cat" command (which isn't really needed), but
maybe you could argue that piping the file to the xargs command is
different from redirecting it from the file, so we'll try it with the
cat command:
$ cat obsolete_clients_list.txt | xargs p4 client -d
Usage: client -d [ -f ] clientname
Missing/wrong number of arguments.
Still doesn't work.
Since this is the GNU version of xargs (which is what Linux comes
with), I can add the "--interactive" flag, and that will show me the
command before executing it:
$ xargs --interactive p4 client -d < obsolete_client_list.txt
p4 client -d bob ted carol alice ?...y
Usage: client -d [ -f ] clientname
Missing/wrong number of arguments.
As you can see, even though I set up my obsolete_client_list.txt file
with one client per line, the "xargs" comand combines all of the
lines, so it only has to execute "p4 client" just once.
The "xargs" command is suppose to save processing time by combining
files with a command, so the command does not have to be executed once
per file. The "xargs" command was really designed to work hand in hand
with the find command and was meant to replace the "-exec" parameter.
For example:
$ find . -name "*.temp" | wc -l
6423
$ find . -name "*.temp" -exec rm { } \;
The above find command will execute "rm" 6,423 times -- once for each
file I want to delete.
By using "xargs" like this:
$ find . -name "*.temp" | xargs rm
I am probably executing "rm" only once -- maybe twice.
Historically, if you needed to execute a command once per "file", you
were suppose to create a simple "read while" loop like I showed in my
original answer. However, in BSD and Linux implementations, a switch
was added to change xargs command line cramming behavior by specifying
a switch on how many arguments you want per execution. In BSD, you
would do this:
$ xargs -n1 p4 client -d < obsolete_client_list.txt
Since I am on a Linux system, I need to do this:
$ xargs --max-args=1 p4 client -d < obsolete_client_list.txt
Client bob deleted.
Client ted deleted.
Client carol deleted.
Client alice deleted.
--
David Weintraub
qazwart at gmail.com
More information about the perforce-user
mailing list