help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] how to understand the xargs


From: lina
Subject: Re: [Help-bash] how to understand the xargs
Date: Tue, 27 Dec 2011 23:12:31 +0800
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111114 Icedove/3.1.16

On Tuesday 27,December,2011 11:02 PM, Greg Wooledge wrote:
On Tue, Dec 27, 2011 at 12:11:59AM +0800, lina wrote:
I don't understand the xargs well,
There's really only one thing you need to know about xargs: never use it.

(Well, OK, I exaggerate very slightly.  It's OK to use it if you supply
an option that suppresses its native uselessness, such as GNU's -0.)

can someone help me understand the below sentence,

ls . | xargs -i -t cp ./{} {}.bak
Never use the output of ls in a script.  Ever.

If your goal here is to make a backup of each file in the current directory,
then do this instead:

for f in ./*; do cp "$f" "$f.bak"; done

Using a glob (like ./*) instead of the output of ls is always better.
Globs are not mangled by ls (replacing non-printable characters with
question marks, failing completely on files with newlines in their names,
etc.), and because you are not using xargs, you will also not have any
of the problems that xargs has with filenames (spaces, quotes).

How does xargs work,
Badly.

imadev:~$ echo 'hi"mom' | xargs echo
xargs: missing quote?: himom

imadev:~$ echo "it doesn't work" | xargs echo
xargs: missing quote?: doesnt work

imadev:~$ echo "this is one filename.txt" | xargs ./bin/args rm
5 args:<rm>  <this>  <is>  <one>  <filename.txt>

ls | xargs -i cp ./{} {basename {}.bak}
definitely not work,
I don't know what that example is supposed to do.  You realize that the
basename command is used to *strip out* directory paths from path names,
or suffixes from file names?  (Also, that is not how you do a command
substitution... but since I can't tell what you were trying to do, I can't
write a working example.)


On Tue, Dec 27, 2011 at 12:25:52AM +0800, lina wrote:
is the help-bash an active list?

seems no reply.
I just got your messages a few minutes ago.

Thanks for your reply.

another question,
$ for i in {1..10} ; do echo $i ; done
1
2
3
4
5
6
7
8
9
10

in command line it works, while on

$ cat a.sh
#!/bin/sh

for i in {1..10} ; do

echo $i

done
$ ./a.sh
{1..10}

why put in a script, it does not work.

BTW, on script I used to use for i in $(seq 10)
just recent trying to learn more, so do some practice.

I will avoid using xargs,

Thanks with best regards,



reply via email to

[Prev in Thread] Current Thread [Next in Thread]