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
Author by
Piyush Mattoo
Updated on July 05, 2022Comments
-
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 over 12 yearsthanks for the tip. But this overwrites the contents of pathToLogFile.log. What if we want to just append to the pathToLogFile.log?
-
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 about 10 yearsIn my testing this doesn't work in
tcsh
, but works inbash
. -
Rajasekhar over 8 yearsThanks for the answer. Some answers didn't work for c shell and it did.
-
Mark Ch over 8 yearson raspberry pi this gave the error "Syntax error: Bad fd number"
-
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 intcsh
under Linux. -
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.