19

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56

3 Answers3

38

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • thanks for the tip. But this overwrites the contents of pathToLogFile.log. What if we want to just append to the pathToLogFile.log? – Sanjeev Kumar Dangi Aug 10 '11 at 08:26
  • 3
    @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 `>>&`. – paxdiablo Aug 10 '11 at 09:49
  • Thanks for the answer. Some answers didn't work for c shell and it did. – Rajasekhar Jul 24 '15 at 11:13
  • on raspberry pi this gave the error "Syntax error: Bad fd number" – Mark Ch Oct 19 '15 at 13:41
  • @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. – paxdiablo Oct 19 '15 at 13:52
  • @paxdiablo my bad, didn't rtfq. I'll leave my comment there anyway, undoubtedly someone else will make the same mistake. – Mark Ch Oct 19 '15 at 14:55
8

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
Edd Barrett
  • 3,425
  • 2
  • 29
  • 48
0

From http://blog.elevenseconds.com/resources/shell-redirecting.html :

(cmd > /dev/tty) >& file1
Chmouel Boudjnah
  • 2,541
  • 3
  • 24
  • 28