?
Solved

Redirect stderr and stdout from MSDOS promt

Posted on 2003-06-25
10
Medium Priority
?
53,551 Views
Last Modified: 2012-08-13
I'm running a perl script from a Windows 2000 Dos Prompt. This script generates output on stdout and stderr. I want to capture both of these into a single file. I can capture them both into seperate files using:

command.pl 1>stdout.txt 2>stderr.txt

What I want to do is:

command.pl 1&2>log.txt

Is this possible?

qbert
0
Comment
Question by:qbert220
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 7

Accepted Solution

by:
billious earned 120 total points
ID: 8797181
From the "Using command redirection operators" HELP on XP (2K should be the same)

Using the <& operator to redirect input and duplicate
To use the redirection input operator <&, the file you specify must already exist. If the input file exists, Cmd.exe opens it as read-only and sends the characters contained in the file as input to the command as if they were input from the keyboard. If you specify a handle, Cmd.exe duplicates the handle you specify onto the existing handle in the system.

For example, to open File.txt as input read to handle 0 (that is, STDIN), type:

<file.txt

To open File.txt, sort the contents and then send the output to the Command Prompt window (that is, STDOUT), type:

sort<file.txt

To find File.txt, and then redirect handle 1 (that is, STDOUT) and handle 2 (that is, STDERR) to the Search.txt, type:

findfile file.txt>search.txt 2<&1

To duplicate a user-defined handle 3 as input read to handle 0 (that is, STDIN), type:

<&3

Using the >& operator to redirect output and duplicate
If you redirect output to a file and you specify an existing file name, Cmd.exe opens the file as write-only and overwrites the file's contents. If you specify a handle, Cmd.exe duplicates the file onto the existing handle.

To duplicate a user-defined handle 3 into handle 1, type:

>&3

To redirect all of the output, including handle 2 (that is, STDERR), from the ipconfig command to handle 1 (that is, STDOUT), and then redirect the ouput to Output.log, type:

ipconfig.exe>>output.log 2>&1



...Not that Gates makes it easy to find!

...Bill (not /that/ Bill)

0
 
LVL 8

Expert Comment

by:K_2K
ID: 8797223
Yes

   command.pl >log.txt 2>&1

From the W2K help file, Command reference, filter section (i believe this also works on older DOS after 3.2 psuedo-copied from POSIX/XENIX, but that is partly a guess):

The >& sign redirects the output from one of the default I/O streams (stdout, stdin, stderr) into another. For example, command >output_file 2>&1 redirects any error messages from the processing of command from the screen to the standard file output. The numeric values of the standard outputs are as follows: Standard output Numeric equivalent
Stdin 0
Stdout 1
Stderr 2

0
 
LVL 7

Expert Comment

by:billious
ID: 8797420
K_2K:

works in DOS 3.2+ - no, not even in DOS6.

> was implemented, but no being able to address stdout differently from stderr. IIRC, stdout was redirected, stderr stubbornly went to the screen (DOS 6 machine not feeling well at present, so I can't check...)


qbert220:
And caution....

using ">" alone, without the stream-specifier needs a space before it, since

command ..... filename.xt2>redir

will be interpreted as
command ..... filename.xt 2>redir

not
command ..... filename.xt2 >redir

...Bill



0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 8

Expert Comment

by:K_2K
ID: 8797620
Bill,

Thanks for the correction on my guess, and i would not have written my post during the 4 to 5 minutes while and after yours was up if i knew yours was there and complete.

Qbert,
plz ignore my answer when closing this to PAQ.  I've seen at least one too many questions lately not following EE's guide of "first correct answer gets it unless later answer was absolutely needed to help finish the fix"

Thanks.

2K
0
 
LVL 7

Expert Comment

by:billious
ID: 8797805
2K: We've discussed this phenomenon. No harm done. I'm no demon-typist so it happens to me, too.

Customer's problem resolved. What it's all about..

...Bill
0
 
LVL 2

Expert Comment

by:happispider
ID: 10295599
Maybe this will help:  The reserved filename "CON" can be used to copy to the screen in Command Prompt.
0
 

Expert Comment

by:mcicogni
ID: 12915401
I'm running a python script (should be quite similar to how a perl script behaves) and I found out that STDIN redirection won't work, no matter how I write the command line.

STDOUT and STDERR redirection work as expected.

Anyone have a clue on what's going on?

Mauro
0
 
LVL 8

Expert Comment

by:K_2K
ID: 12924576
As expected using Bill's syntax to get them to and from Python?  or as expected inside Python?  If you want different commands inside your python code to redirect from different files or handles, you should ask a fresh question in the Python or Pearle subject area.


Here's an example of a "hello world" dirivative that asks your name, with "Set /P" looking at STDIN:  You could write this in VBScript, Python, C#, or COBOL, but since we are in the MSDOS subject area, I'll choose that:
:::::::::::::::::::
:: test.cmd
::   test stdin redirection
echo off
setlocal
set zx=
set /p zx=Enter name please:
echo.
echo hello %zx%
:: end of test.cmd
:::::::::::::::::::


Now to use that from DOS in a way that demonstrates DOS STDIN redirection:
:::::::::::::::::::
C:\batch> echo harry > test.txt

C:\batch> test.cmd
Enter name please: tom

hello tom

C:\batch> test.cmd 0<test.txt
Enter name please:
hello harry
:::::::::::::::::::


Note that is all the same no matter which language the program is in, although we could create a pipe instead of using redirection:

:::::::::::::::::::
C:\batch> type test.txt | test.cmd
Enter name please:
hello harry
:::::::::::::::::::


So why bother?
Suppose I wanted the batch itself to default from a setup file, in case you do not answer.  That way the customer can change the default without opening the program:
:::::::::::::::::::
:: test.cmd
::   test stdin redirection
echo off
setlocal
0<test.txt set /p zx=
set /p zx=Enter name please (default %zx%) :
echo.
echo hello %zx%
:: end of test.cmd
:::::::::::::::::::


Now harry only needs to press enter (at least until tom changes the default file):
:::::::::::::::::::
C:\batch> test.cmd
Enter name please (default harry ) :

hello harry

C:\batch> test.cmd
Enter name please (default harry ) : tom

hello tom
:::::::::::::::::::


You should be able to plug your python command lines into a batch wrappper similar to that first set line, with as many different file names and python scripts as you need in one wrapper.


Hope This Helps (\o/),
2K
0
 

Expert Comment

by:mcicogni
ID: 12924895
Hi, thanks for your comments.

I know it could be a different question however I found this thread while searching for good guesses to my problem and thought I'd add a comment here because I felt this really isn't a Python question.

Why? Because with "work as expected" I meant that if I wrote a command line like

C:\> script.py <inputfile >outputfile

I'd expect my script to get whatever is inside "inputfile" (assuming it exists) on the standard input, and to write it standard output into a freshly created (or overwritten) file called "outputfile".

What I get instead is that the script will raise an exception no matter what, lamenting that its input is non-existent.

If I write the script to also parse its command line so that I can write

C:\> script.py inputfile >outputfile

i.e. "inputfile" is an argument, and I let the script open and read the named file, I can in fact test that stdout redirection works (and BTW STDERR redirection also works too).

So my guess was that there could be some quirks in how the Windows command shell does things, and I posted the comment.

Meanwhile, experimenting around, I found out that if I invoke the interpreter explicitly, instead of relying on Windows' shell resolution, i.e.

C:\> python.exe script.py <inputfile >outputfile

then STDIN redirection works, too.

I wonder if this happens with any script interpreter (Perl, TCL, Ruby, etc.) or if it really is Python's fault.
0
 
LVL 8

Expert Comment

by:K_2K
ID: 12930061
true,  this is a DOS question and related to this thread,  which is why I answered.  my line about the new question was in case you were having trouble in python, which i did not think was the case but possible.  Now i c you obviously have no such problem.  


I would try each of the following,  as the Python interpreter may be using specific handles when called by way of association as opposed to directly stating the .exe:

C:\>  script.py 0<inputfile >outputfile
C:\>  script.py 1<inputfile >outputfile


Now,  why does it work differently?  well,  you may or may not find that answer in the ftype command.  First enter:
C:\> assoc  .py

.py=pyfile

Then, use the value in the following.  For clarity we'll assume the above result, but you'll most likely need to adjust:
C:\> ftype  pyfile

Now THAT is the command that runs when you call script.py without telling it what program should be used.  For example, if
    pyfile=python.exe "%1" "%2"
then your first two parameters are being quoted and the rest ignored when you use "script.py" but not when you use "python.exe script.py"  
Not to mention, there could be two versions of the python interpreter, and the association may point to an ftype that calls the other one.
If there are other options on that command line, use python's documentation to figure it out,  or change it as you wish.  I recommend writing it in 3 separate easy to find places and mark them all "original" before changing it.

Hope this helps.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question