0

I’m trying to answer this lab question. Is there a way to solve this without using tee command?

As user1 on server1, run the ls command on /etc, /dvd, and /var. Have the output printed on the screen as well as redirected to file /tmp/ioutput, and the errors forwarded to file /tmp/ioerror. Check both files after the execution of the command and analyze the results. (Hint: Input, Output, and Error Redirections).

If I tried like this, the output is not printed on the screen.

[user1@server1 ~]$ ls /etc /dvd /var 1> /tmp/ioutput 2> /tmp/ioerror

If I used with tee command, error file was empty.

ls /etc /dvd /var | tee /tmp/ioutput 2> /tmp/ioerror

Is there a way to solve it only by using redirection operators?

1
  • @Quasimodo, Thanks for your clarification. However, I think it should be in a single command.
    – Zar
    Commented Mar 9, 2021 at 13:55

1 Answer 1

3

Just use tee. But to get the standard error output, you need to redirect the standard error of the original program. The pipe (|) is only passing the standard output to tee, that's why the error file is empty. What you wanted is:

ls /etc /dvd /var 2> /tmp/ioerror | tee /tmp/ioutput 

If you want to see both stderr and stdout on screen, try this:

ls /etc /dvd /var 2> >(tee /tmp/ioerror) | tee /tmp/ioutput 

That will first redirect stderr to tee /tmp/ioerror, which means you will both see it on screen and have it in the file, and then it pipes the stdout to tee /tmp/ioutput ensuring that too is both printed and saved to the file.

3
  • thanks for your kind and quick support. Yes, your first method is ok to write in both files and also display the output only on the screen. It’ll be enough for my lab answer. Then, if I tried to see both on screen, the error file was still empty.
    – Zar
    Commented Mar 9, 2021 at 13:51
  • @Tun sorry, I was missing an extra >, try the updated answer.
    – terdon
    Commented Mar 9, 2021 at 13:55
  • arr, yes, it's ok now. thanks again for ur updated answer.
    – Zar
    Commented Mar 9, 2021 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .