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.
To redirect standard output and standard error to the same file, use the following command syntax. Specifically, append 2>&1 to the end of your usual command. A slightly easier way to achieve this functionality is with the &> operator.
Normally, if you want to run a script and send its output to a logfile, you'd simply use Redirection: myscript >log 2>&1. Or to see the output on the screen and also redirect to a file: myscript 2>&1 | tee log (or better still, run your script within the script(1) command if your system has it).
The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With