Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect Standard Output/error to log file

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.

like image 845
Piyush Mattoo Avatar asked Jan 18 '11 07:01

Piyush Mattoo


People also ask

How do you redirect output to error as well as output to file?

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.

How do I redirect a script to log file?

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).

How do I redirect standard output?

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.


2 Answers

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.

like image 196
paxdiablo Avatar answered Sep 18 '22 16:09

paxdiablo


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
like image 40
Edd Barrett Avatar answered Sep 19 '22 16:09

Edd Barrett