2

If I use the text re-directors > and >> they output to a text file which is fine for long term logging but, I also need them to be present on screen in the traditional Standard Input, Standard Output format as I'm running code through the shell (bash) function in Ansible.

Using >> makes Ansible believe it was a successful operation even if it failed which is undesirable. Any help on how to achieve the having your cake and eating it too scenario?

Raffa
  • 34,963
Michael
  • 51

2 Answers2

6

One way is to pipe it to tee -a /dev/tty >> file like so:

echo "This is some text" | tee -a /dev/tty >> file

Another way is to pipe it to tee -a file with no redirects like so:

echo "This is some text" | tee -a file

A third way(if you need >> directly after the command) is to redirect to /dev/tty(not saved to file though) like so:

echo "This is some text" >> /dev/tty

If that is not enough, then you can use named pipes(two terminals are needed for this process) by creating a named pipe called e.g. my_pipe in one terminal like so:

mkfifo my_pipe

Then, redirecting your output to it like so:

echo "This is some text" >> my_pipe

Then, from another terminal read that named pipe and do whatever you want with the output like so:

cat my_pipe | tee -a file

or like so(to keep the pipe waiting/open for reading between writes):

tail -F my_pipe | tee -a file

Keep in mind that paths apply to named pipes like any other file ... so make sure you provide the correct path to the named pipe when you use it.

Raffa
  • 34,963
1

I found this to be a suitable solution but am curious if there are better ways.

echo "Hello" >> file && cat file
Michael
  • 51