On 12/09/2010 01:17 PM, Jan L. Peterson wrote:
> On Dec 9, 2010, at 3:21 PM, Paul Graydon wrote:
>> On 12/09/2010 12:07 PM, Randal L. Schwartz wrote:
>>>>>>>> "Jan" == Jan L Peterson<[email protected]> writes:
>>> Jan> (eval `ls -1 *.meta | while read m; do f=\`grep "^Filter:" $m | awk
>>> '{print $2; exit}'\`; echo -n "./$f -r \`basename $m .meta\` |"; done | sed
>>> -e "s/|$//"`)< input> output
>>>
>>> Jan> which gets rid of the multiple "cut" commands, "head", one of the
>>> Jan> nested loops, and both gratuitous uses of "cat".
>>>
>>> But still leaves the evil of "ls *wild*card*", which is almost always
>>> wrong, unless what you're trying to do is get the contents of many
>>> subdirectories whose name matches a pattern.
>>>
>> If you're going to do such a thing I would have thought find would have
>> been the better command to use, using its exec flag to make sure file
>> names with spaces or other odd characters get handled correctly.
>>
>> I was always taught that pushing output from ls to another command is
>> wrong, wrong, wrong, and the few times I've done it in a fit of
>> stupidity has always backfired on me!
> Randal and Paul,
>
> I was never taught that this was wrong. I've often piped the output of ls
> into a while loop when I've needed to do something along these lines. Please
> educate me as to how "ls -1 *.meta" is wrong in this case. I would think
> find is especially wrong because it descends directories.
>
> Also, can you suggest any alternate ways to get a list of files ending in
> .meta that doesn't use a wildcard?
>
> In any case, the choice of command came directly from the original poster's
> example. I would hope he (waves at David) knows what the structure of his
> directory is and what files are expected to be present or not present.
>
> -jan-
Here's a quick example of how piping the output from ls can be wrong, in
a safe way:
[13:21:29 p...@paul ~/demonstration]$ ls -1
[13:21:32 p...@paul ~/demonstration]$ echo "Hello" > test\ file
[13:21:38 p...@paul ~/demonstration]$ ls -1
test file
[13:21:40 p...@paul ~/demonstration]$ ls -1 | xargs cat
cat: test: No such file or directory
cat: file: No such file or directory
[13:21:48 p...@paul ~/demonstration]$ find . -type f -exec cat {} \;
Hello
[13:21:59 p...@paul ~/demonstration]$ ls -1 | rm
rm: missing operand
Try `rm --help' for more information.
Obviously in part it depends on what you're doing with the ls output,
but you have to remember that it won't escape spaces or other special
characters. Even if you can't imagine a circumstance under which a
special character would be part of the file name it's generally safer
not to gamble on it, just in case. I've seen cases where serious damage
was done as a consequence of ls output being piped off to other commands :)
Paul
_______________________________________________
Discuss mailing list
[email protected]
https://lists.lopsa.org/cgi-bin/mailman/listinfo/discuss
This list provided by the League of Professional System Administrators
http://lopsa.org/