Append a file, output to screen and redirect from stderr all at once, without tee

I’m running a time dd command in a shell script, and I’d like to output the results to a file, and print it out on the screen. The line that I’m currently running is:

(time dd of=$dest_filepath if=$src_filepath bs=$block_size count=$block_count) >> $log_file 2>&1 &

Although that is leaving me with an empty file and doesn’t output to screen. What would I need to do in order to do all three things?

Note that I’m running this in an embedded system with a Busybox installation which does not include tee so this isn’t a duplicate of this question.

Answer

What you’re showing works as expected on my system. Are you sure you’re using bash and not sh? In any case, I tried with dash and with busybox‘s sh and it worked there too. In the absence of tee, I think the only solution will be to cat $logfile after the command is finished.

Another possibility would be to make a link to busybox called tee and attempt to run that. The busybox that came with my Debian supports that but I don’t know if yours will:

ln -s /bin/busybox /bin/tee

Then, try running tee normally.

If you really can’t get tee, your only other option would be something like this:

foo=$( ( time dd if="file1" of="file2" bs=12 count=5 ) 2>&1 &)
echo "$foo"
echo "$foo" >> logfile

Leave a Reply

Your email address will not be published. Required fields are marked *