1

I have a BASH utility that outputs measurements to screen live as it runs and writes the result to file at the same time. I am having to repeat the same code twice (see below).

Once to write to screen, and

Once to write to file.

This seems like a lot of redundancy to me.

Can I put one text block into something like a function then call it to be written to screen and file at the same time?

This would cut down on a lot of re-keying.

Example below

###### write out to file : push end time at end ################
echo >> $file_name
echo "End time: "$end_time >> $file_name
echo >> $file_name
echo >> $file_name
echo >> $file_name

######  print to screen : push end time at end  #################

echo 
echo "End time: "$end_time 
echo
echo
echo

So I'd be looking for someting like this

funtion text_block{
######  print to screen : push end time at end  #################
echo 
echo "End time: "$end_time 
echo
echo
echo
}

Then a command that does something like this

"command print text_block to screen, print text_block to file"

Kiwy
  • 9,415
  • 13
  • 49
  • 79
Kes
  • 737
  • 1
  • 8
  • 20

1 Answers1

2

You can use the utility tee

command | tee my_file.out  

this will both write a file name my_file.out and write it to stdout.

Source this post from StackOverflow

Kiwy
  • 9,415
  • 13
  • 49
  • 79