1

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.

Yann
  • 1,170
  • 8
  • 13
  • Have you looked for `man tee` – Valentin Bajrami Oct 15 '14 at 11:00
  • 1
    Why don't you have `tee`? What OS are you using? What output do you expect in your file? The `time` or the `dd` or both? – terdon Oct 15 '14 at 11:00
  • It's a cut down version of Linux running on busybox. I don't have it because of how cut down the version was - I didn't have bash until I explicitly enables it and rebuilt. I'm expecting both time and dd to output to file. @terdon – Yann Oct 15 '14 at 11:03

2 Answers2

3

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
terdon
  • 234,489
  • 66
  • 447
  • 667
  • I'm not sure that `cat`ing it is really a viable option, as I'm running the command thousands of times, so catting the file would print all of the outputs every time. – Yann Oct 15 '14 at 11:17
  • @Yann4 did you try linking to `busybox` as `tee`? In any case, you're working with a minimal system, you can expect some loss of functionality. If you can't get `tee` to work, there's no way around catting. The only other choice would be to save the output as a variable, echo it and then print it to the file. – terdon Oct 15 '14 at 11:20
  • It's not on the system, I'd have to recompile the kernel to get it there. – Yann Oct 15 '14 at 11:22
  • The Busybox shipped by Debian includes most utilities with most features. It isn't a reliable indicator of what might be found in Busybox instances on very small systems. A lot of things are optional when you compile Busybox. If the tee utility had been included, the `tee` command would presumably exist already. For example, my router's Busybox has 97 utilities, while Debian's has 200. – Gilles 'SO- stop being evil' Oct 15 '14 at 22:49
-2

You can use the tee command. Here i have grouped the commands time and dd using the code block so that they will be treated as a single command and their output can be handled easily.

{ time dd of=$dest_filepath if=$src_filepath bs=$block_size count=$block_count; } 2>&1|tee $log_file

Make note of the ; at the end of the second command. This is mandatory for code blocks to work.

Incase if you dont want to use tee then you might think about this

{ time dd of=$dest_filepath if=$src_filepath bs=$block_size count=$block_count; } 2>&1 &> $log_file;cat $log_file
Kannan Mohan
  • 3,191
  • 2
  • 17
  • 16