Re: Bash question: get output as a variable?
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.
array=( $(tar -tf archive.tgz) )
# or
tar -tf archive.tgz > file.list
# now do whatever work with the file list, either from the array or
# from the file.
: ...
Best regards
Mart
Reply to: