vote up 1 vote down star
1

Hi,

Back in university, when we had to submit an assignment in CS, we would have to perform a series of steps including running script, date, whoami, etc., and then running our program.

The script command would pipe all text sent to the display to both the display and a specified file as well.

Ever since, I have been looking for a Dos and/or Windows version, but have come up empty. Some programs can be redirected to a file, but then the display is not echoed, and some programs don’t seem to work with redirection at all.

Any ideas?

Thanks.

Edit So far, the answers that I’ve gotten seem to work exactly like the standard redirection commands (<, >, |). These do not work with all programs. For example, the Microsoft C++ compiler CL.EXE. If you run cl /? through a redirection command or pipe it through another program (such as TEE), you will not get the header/banner text.

Another example is a program I wrote a while back in Pascal (I think the last compile was in FreePascal). The help text does not get redirected at all. I have seen this occur with other programs as well like MKISOFS. It has a long help text, but cannot be paused by piping it through MORE or redirected to a file!

I have wondered about this for many years. I used to think that it may be because the text is being written directly to the screen (eg port B800) or something, but I have yet to pin down the cause, let alone find a program that can do this job.

flag

40% accept rate

4 Answers

vote up 1 vote down check

This General Pascal FAQ explains the cause, as well as a solution for Turbo Pascal programs.

Q. When I redirect the screen output of my programs to a file the file is empty and the output still appears on the screen. What am I doing wrong?

A. You are probably using the CRT unit and its default method of writing to stdout is by direct screen writes. In order to enable output to be redirected all writes must be done by DOS. Setting the variable DirectVideo to false has no effect on redirection as all it does is use the BIOS for screen writes - not DOS.

To enable redirection you must not use the CRT unit

OR

assign(output,'');
rewrite(output);

This will make all output go through DOS and thus can be redirected if desired. To restore the default situation:

AssignCRT(output);
rewrite(output);
link|flag
Thanks for the link to the FAQ. I checked my sources and it seems that you are correct. The version of the program that I use was compiled back when I had the CRT unit included, so it doesn’t redirect. The current version does not include the CRT unit (in fact it is commented out, apparently because I had discovered it was the cause of the redir problem before but forgot). I compiled and tested the latest version and it redirects correctly. As for the other apps like CL, it seems you were both correct, it redirs to STDERR, so now I can use the SCRIPT.exe command as expected. Thanks. – Synetech inc. Nov 15 at 17:50
vote up 1 vote down

Take a look at Cygwin, it gives you access to all those great UNIX command line tools in Windows.

link|flag
Thanks, but it has the same problem that I explained in the comment to akf’s answer. – Synetech inc. Aug 4 at 16:54
vote up 1 vote down

Unfortunately, it seems that you won't have luck finding an out-of-the-box Microsoft solution. You can check out a similar post on StackOverflow. The digest:

link|flag
I tried them out. Unfortunately they do not work any better than standard redirection. For example, the MS C++ compiler CL.exe. If you run <code>cl /?</code> and run it through < or > or | or TEE or anything else, then you will only get the output from the options on, not the header text. – Synetech inc. Aug 4 at 16:53
6  
You probably don't get the header text because it is sent STDERR. You'll have to redirect both STDOUT (>) and STDERR (2>). – Ludwig Weinzierl Aug 4 at 17:30
2  
FYI, this is how you redirect both STDERR and STDOUT for cl.exe: cl /? >output.log 2>&1 – wonsungi Nov 14 at 8:12
vote up 1 vote down

I found a Cygwin port of the Unix script command. It captures both STDOUT and STDERR (so it gets the header output from cl.exe).

However capturing cmd.exe commands is a little convoluted for two reasons:

  • script spawns a cygwin bash shell (not cmd.exe)
  • script does not work when started from a cmd.exe shell; it must be started from cygwin.

You can do it, though:

  1. open a cygwin shell.
  2. start script
  3. start a cmd.exe shell from within the cygwin shell
  4. do your stuff in cmd.exe
  5. exit out of cmd.exe
  6. exit out of script

*also the cmd.exe shell spawned from inside cygwin acts a little strange, but the commands seem to work:

  • the window that usually pops up if you try to run cl.exe without first running vcvars32.bat does not come up
  • console input is very finicky (for example typing LEFT RIGHT cl or UP cl does not work.)
link|flag
Thank wonsungi. I eventually managed to get the script.exe util from Cygwin to work, but I still have the same problem. I was able to get both parts of *CL*’s output to redirect to a file with either script.exe or the redirection syntax you listed above. Unfortunately, there are still programs that output text to the screen that still do not get redirected by any of these. One such program is a Pascal app I wrote some time back (it prints using the standard writeln). The current one was compiled with either Free Pascal or Turbo Pascal; I’ll have to check and try both. – Synetech inc. Nov 14 at 18:03

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.