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

Re: Bash question: get output as a variable?



On 06.02.2010 13:39, Mart Frauenlob wrote:
> On 04.02.2010 23:09, Dotan Cohen wrote:
>> I'm scripting a backup solution, the line that does the business looks
>> like this:
>>
>> tar -zcvf - *  --exclude-from $EXCLUDES  | openssl des3 -salt -k $1 |
>> dd of=$(hostname)-$(date +%Y%m%d).tbz
>>
>> Because of the "v" flag tar writes to stdout the name of each file
>> copied. How can I get that output redirected to a variable, to use
>> later in the script?
>>
>> Thanks!
>>
> 
> After reading many of the follow-ups, I'd suggest something like that:
> 
> #!/bin/bash
> 
> # create the archive local first, as if you use 'tar -v' and redirection
> # of stderr, you will also catch (possible) error messages of tar, not
> # only a file list.
> 
> tar -czf archive.tgz
> 
> # now list the content with 'tar -t'.
> # that ensures that we work only with files, which are in the archive.
> # you can put it into an array, or if that's not save enough (as the
> # array could become quite big and consume a lot of memory) redirect
> # the output to a file.
> 

forgot:
oifs=$IFS
IFS='$\n'

> array=( $(tar -tf archive.tgz) )
> 
> # or
> tar -tf archive.tgz > file.list
> 


If filename contains newline the whole thing still is in trouble though.



Reply to: