[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!
> 

I would avoid trying to store the file listing in any type of variable,
since you have no idea how big the list will be.

And, as others have noted, in the above case the file names list is sent
to stderr, so it doesn't mess up the stdout you requested for the
archive data.

I'd recommend using a "named pipe", or FIFO, like this:

  mknod filenames p
  tar -zcvf - *  --exclude-from $EXCLUDES 2>filenames  |
  openssl ... | dd ... &

NOTE:  the '2>filenames' and the ampersand to put the command into the
background.

Then, later in the script, you would do something like this:

while read filename
do
  # other work
done < filenames

I've not done the above with anything large enough to know, but the way
a FIFO works, the read loop will block, waiting for input, if it
exhausts the FIFO content.  The FIFO will generate a zero length read
after the last line from tar is captured which will terminate the loop.

-- 
Bob McGowan


Reply to: