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?

5
  • @muru The duplicate you linked to doesn't answer how to use >> directly after the command and have the output on screen as well, which seems to be an essential part of the OP :-)
    – Raffa
    Jun 28 at 5:32
  • @Raffa why would >> be essential here?
    – muru
    Jun 28 at 5:34
  • @muru Can't say for sure ... Not my question ... but, "Using >> makes Ansible believe it was a successful operation even if it failed" is in the question anyway ... I would suggest you ask the asker :-)
    – Raffa
    Jun 28 at 5:37
  • @Raffa Ok, but that sentence is followed by "which is undesirable" (bold mine), so if anything, not having >> seems to be better. (Though I think OP's somehow misunderstanding Ansible's operation here.)
    – muru
    Jun 28 at 5:40
  • @muru "seems" ... I couldn't agree more :-)
    – Raffa
    Jun 28 at 5:42

2 Answers 2

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.

2
  • 1
    blah | tee file will put the output into file and on stdout.
    – doneal24
    Jun 27 at 17:52
  • @doneal24 I know ... but, OP seems to need to use redirection > and >> ... so I am trying to give alternatives :-)
    – Raffa
    Jun 27 at 17:55
1

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

echo "Hello" >> file && cat file
3
  • 3
    this will cat the entire file, including whatever was there before you appended to it with >>
    – Esther
    Jun 27 at 19:27
  • tail maybe a good substitute then.
    – Michael
    Jun 28 at 10:19
  • 1
    any of the other answers that Raffa gave.
    – Esther
    Jun 28 at 13:18

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