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

Re: Bash question: get output as a variable?



Ken Teague wrote:
> On Fri, Feb 5, 2010 at 10:10 AM, Chris Jackson
> <c.jackson@shadowcat.co.uk> wrote:
>> Use $() like you do with the date command. You have to redirect stderr back
>> to stdout, which means running it in a subshell:
>>
>>
>> FILES=$( ( tar -zcvf - *  --exclude-from $EXCLUDES  | openssl des3 -salt -k
>> $1 | dd of=$(hostname)-$(date +%Y%m%d).tbz ) 2>&1 )
>>
>> It may cause unexpected results if there're spaces in the filenames though.
> 
> I think this is the best method I've seen thus far.  Putting the data
> into an array should avoid problems with file names containing spaces.
>  I don't understand why he would stderr in it, though.  I'd like to
> know what the OP plans to do with the list of processed files.
> 
> 

As noted in some earlier posts, using 'tar -f -' causes the archive tar
creates to be written to stdout.

This means the list of file names printed because of the 'v' option,
must be written to some other file descriptor, else it will corrupt the
archive contents.

The "usual" way for an app to do this is to write to stderr, which is
what 'tar' does.

The tar sdtout is already redirected in the initial pipeline.  Putting
the pipeline in parenthesis causes the entire thing to run in a
subshell.  The '2>&1' takes the stderr of the subshell and puts it onto
the stdout of the current shell, which is then redirected by the $(...)
into the variable.

-- 
Bob McGowan


Reply to: