0

so for a question I have to store both messages of x and y to log, since y only exists and x does not I used cat x y &> log to be able to store both messages. However as the second part my professor wants us to use tee to store both messages and output both messages on the screen, i have tried things like cat x y &> log | tee log ,,, tee x y &> log ,,, log | x y &> log | tee log but can't seem to get it to work at all, I even google search how and have absolute no clue, anyone have anything that could help?

  • tee is a command; &> and | are both redirection operators. tee never sees the output of cat in this case, because you already redirected the output of cat to a file. Try cat x y | tee log – Wildcard Mar 30 '16 at 01:02
  • If you don't have anything in file x and you don't have anything in file y then of course you don't see anything. Perhaps you meant to use echo instead of cat. – Wildcard Mar 30 '16 at 01:07
  • Please read through the answer I linked to. (&> is not a standard redirection operator, by the way, it is specific to bash.) Anyway &> filename means the same thing (in bash) as 2>&1 > filename means in any shell. What you want to do instead is redirect stderr to the same place as stdout, then pipe stdout into tee. Read the post I linked. – Wildcard Mar 30 '16 at 01:13
  • my apologies i made mistake that earlier comment on cat x y | tee log worked fine i found the problem on why it didn't, sorry and thanks for the help. – Phantom1421 Mar 30 '16 at 02:12
  • 2
    @Wildcard: &> filename means the same thing (in bash) as > filename 2>&1 (or 2> filename >&2) *but not* 2>&1 > filename — that’s different; order matters (and can sometimes be difficult to understand). – G-Man Says 'Reinstate Monica' Aug 10 '16 at 01:35
  • @G-Man, mea culpa, you're right. Must have been pretty tired when I wrote that to have missed it.... – Wildcard Aug 10 '16 at 01:57

1 Answers1

1

If I understand your comments correctly, you have something like the following scenario:

  • file y contains some text
  • file x doesn't exist
  • file log doesn't exist (or you don't care if you delete its contents with the command I give you).

You want to have the error message about the nonexistence of file x and the contents of file y both dumped into the file log, and you also want this output displayed on your terminal.

If that's all correct, what you want is:

cat x y 2>&1 | tee log

(NOTE: If this doesn't do what you want, edit your question to include the actual error message you got, and explain clearly how it is different from the result you wanted.)

Wildcard
  • 36,499
  • my apologies i made mistake that earlier comment on cat x y | tee log worked fine i found the problem on why it didn't, sorry and thanks for the help. you can edit answer so it looks like that. – Phantom1421 Mar 30 '16 at 02:13
  • @Phantom1421 the earlier command cat x y | tee log will not put the error message from cat into the file log. The command in my answer will. – Wildcard Mar 30 '16 at 04:57