Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to read a file from a given offset until the end of that file.

I need to retrieve the number of bytes that were read during the process and also to redirect the output of the file elsewhere.

Here is my script:

...some stuff here...
dd if=$file bs=1 skip=$skippedBytes | tee >(wc --bytes > $file.count) >(cat - >> $file.output) | $($exportCommandString $file)
byteCount=$(cat $file.count)
rm $file.count
echo "Number of read bytes: $byteCount"

I would like the "wc --bytes" part to put its returned value inside a variable so I can use it after, without using a file ($file.count).

Something like:

dd if=$file bs=1 skip=$skippedBytes | tee >(byteCount=$(wc --bytes)) >(cat - >> $file.output) | $($exportCommandString $file)
echo "Number of read bytes: $byteCount"

Except that doing this, my script hangs and does not work.

Is it possible to do this and how ?

share|improve this question
    
This problem was solved but is linked to the following one: unix.stackexchange.com/questions/67494/… –  lauhub Mar 10 '13 at 19:38

2 Answers 2

up vote 3 down vote accepted

You can use a small hack with redirections:

byteCount=$( exec 3>&1 ; 
     dd if=$file  bs=1  skip=$skippedBytes | tee -a >(wc -c >&3) $file.output |\
     $($exportCommandString $file) > /dev/null ;  3>&1   )

It redirects all output to 3, that you've created with exec, and then returns it back to one at the end.

You also need to redirect all output from $exportCommandString to /dev/null, otherwise it will be mixed with wc output.

All stderr will work as usual, there is no any changes.

p.s.: you can use tee -a file instead of tee >(cat - >> file)).

p.p.s.: You can't export variables from subshell, which is always created while using pipe | in bash or $(). So there is no way to make something like

tee -a >(VAR=$(wc -c)) $file.output
share|improve this answer
    
I corrected a small typo (missing closing parenthesis). Thank you for this complete answer. –  lauhub Mar 10 '13 at 18:44

Maybe something like this:

byteCount=$(tail -c +$skippedBytes $file | tee $file.output | wc -c)
share|improve this answer
    
Not exactly what I had expected, but it works too. Thanks –  lauhub Mar 10 '13 at 18:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.