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

Re: Bash question: get output as a variable?



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!



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.

--
Chris Jackson
Shadowcat Systems Ltd.


Reply to: