0

I am trying to implement something which my logic says can't be done. But I need your help to understand why can't it be.

Short Version of Question

Is it possible to log stdout+stderr of a script in csh without using file redirection ( >& or tee ).


Detailed Explanation of Question

I have a requirement with a csh script (script1) where I am not allowed to use file redirection.(I will give the reason in a while) So that means I can't use something like

echo just checking >& logfile

hence I can't use this or tee to create my logfile. I also have a another script (script2) which is a top level script.

I can either run script1 in standalone mode or through script2.

In either case i need to create a log(stdout+stderr) of script1 in logfile.

There are two possible(but not complete) option for that

write this line in script2

./script1 >& logfile

But then I can't log script1 in logfile when script1 is run in standalone mode.

Another option is to use file redirections in script1 like this:

echo test starting >> logfile
echo test over

In this case thee are two disadvantages: 1) "test over" prints before "test starting" , i.e. the order of occurring of command logs is not certain. 2) It's tedious to put >>& after every statement if I am intending to cover whole script.

Now is there any other way,I can get what I need. That is I can run script1 without file redirection and still get to log its stdout+stderr in logfile.

Ani
  • 918
  • 1
  • 10
  • 25
  • I do not understand. You say "I am not allowed to use file redirection" but then you say "Another option is to use file redirections in script1". The other option seems to be exactly the same as the example you are not allowed to use. – William Pursell Apr 18 '12 at 17:56
  • I am sorry for not being clear. By the "another option.." i was explaining why i can't use the normal file redirection, as Its not giving the desired output. Please prompt me if you need more inputs. – Ani Apr 18 '12 at 18:27

1 Answers1

2

You mention csh, so this may not help you. On the other had, it may motivate you to stop using csh for scripts, a task for which it is notoriously inappropriate. In sh, you can simply do:

#!/bin/sh
exec > logfile 2>&1
echo foo

To write foo (and the output and errors of all subsequent commands) to the logfile

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Unfortunately i can't use anything other than csh/tcsh. I do appreciate your answer William. So, what you mean is exec statement doesn't work with csh? – Ani Apr 18 '12 at 18:30