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

Re: Bash question: get output as a variable?



On Fri, Feb 5, 2010 at 7:44 AM, bruno <bruno.debian@cyberoso.com> wrote:
> because it's a simplier way to get the list into a variable

Can you please explain how it's simpler.  The method I suggested
certainly isn't as easy, but the method I suggested merely showed a
detailed example.  Others here are coming up with 1 or 2 liners that
can do this without having to process the archive a 2nd time.

I don't see how the example you provided puts the list into a
variable.  Would you be so kind as to go into more detail regarding
your suggestion?


> because the script gets much clearer

I agree that your suggestion would be much clearer, and your
suggestion may suit the needs of the OP.  I suppose it's just my
personal feelings to not sacrifice efficiency for making a script
easier to understand.  Comments can be added to the script to explain
how parts of it function.


> because it is not safe to rely on stderr since it is supposed to display
> errors, which it might do as well

Others have recommended stderr.  I'm not sure why.  The method that
would be most effective would be to direct stdout, not stderr.

itsme@mybox:~$ ls -l
-rw-r--r-- 1 itsme itsme     0 2010-02-05 14:54 file1
-rw-r--r-- 1 itsme itsme     0 2010-02-05 14:54 file2
-rw-r--r-- 1 itsme itsme     0 2010-02-05 14:54 file3
-rw------- 1 root   root        0 2010-02-05 14:53 somefile
itsme@mybox:~$ tar cvf test.tar somefile file1 file2 file3 >out.txt
tar: somefile: Cannot open: Permission denied
tar: Error exit delayed from previous errors
itsme@mybox:~$ cat out.txt
file1
file2
file3


> because he would get the list of the files that have really been compressed,
> not just the processed ones

Not if he uses stdout.  The example shown above only shows what was
added to the tar file.


> because the overhead may not be worth the complication of trying to do both
> things at the same time

Efficiency vs clarity; sometimes it's a personal preference, other
times it may not be.  For example, if I'm passing this script on to
someone else that isn't familiar with the scripting language, I think
clarity would be paramount.  Otherwise, I'd go for efficiency.


> because he might as well never use the v option (it's not useful in my
> proposition either, and it would even reduce the overhead)

The method you suggested is to process the archive a 2nd time to get
the listing, and to use -tv.  That causes more overhead than getting
the list of processed files while the archive is being created.


Reply to: