Redirect Standard Output/error to log file

38,930

Solution 1

For a start, it wouldn't be:

./ShellFile.sh 2>&1 | pathToLogFile.log

since that would try and pipe your output through the executable file called pathToLogFile.log rather than sending the output there.

You need:

./ShellFile.sh >& pathToLogFile.log

which redirects both standard output and error to the file.

Solution 2

On a side note, tee(1) may be of use if you want to see output both on the terminal and in a file.

  ./script 2>&1 | tee logfile.txt
Share:
38,930
Piyush Mattoo
Author by

Piyush Mattoo

Updated on July 05, 2022

Comments

  • Piyush Mattoo
    Piyush Mattoo almost 2 years

    Looking for a way to redirect std error and std output to a log file in Tcsh shell.

    Tried ./ShellFile.sh 2>&1 | pathToLogFile.log and got the error "Ambiguous output redirect"

    Would appreciate any inputs.

  • Sanjeev Kumar Dangi
    Sanjeev Kumar Dangi over 12 years
    thanks for the tip. But this overwrites the contents of pathToLogFile.log. What if we want to just append to the pathToLogFile.log?
  • paxdiablo
    paxdiablo over 12 years
    @Sanjeev, then you would ask your own question, with the specific requirements, rather than trying to ask a question in a comment field which very few people will see :-) That way, SO functions as intended with specific answers to specific questions. As a hint, you may want to look into >>&.
  • Stuart
    Stuart about 10 years
    In my testing this doesn't work in tcsh, but works in bash.
  • Rajasekhar
    Rajasekhar over 8 years
    Thanks for the answer. Some answers didn't work for c shell and it did.
  • Mark Ch
    Mark Ch over 8 years
    on raspberry pi this gave the error "Syntax error: Bad fd number"
  • paxdiablo
    paxdiablo over 8 years
    @MarkCh, my first thought would be either that you're not running tcsh or the version under RaspPi is deficient somehow. It works fine in tcsh under Linux.
  • Mark Ch
    Mark Ch over 8 years
    @paxdiablo my bad, didn't rtfq. I'll leave my comment there anyway, undoubtedly someone else will make the same mistake.