BUG: xp_cmdshell Does Not Display Last Line of Command Output (255749)



The information in this article applies to:

  • Microsoft SQL Server 6.5
  • Microsoft SQL Server 7.0

This article was previously published under Q255749
BUG #: 18996 (SQLBUG_65)
BUG #: 57628 (SQLBUG_70)

SYMPTOMS

The last line of a command's output is not returned by the built-in extended stored procedure xp_cmdshell if that line does not end in a carriage return/line feed (CR/LF).

WORKAROUND

To work around this problem, add a CR/LF to the end of the data sent to stdout by the executed command.

The following two workarounds assume that you are trying to capture the output of the following command:
   exec master..xp_cmdshell 'someapp.exe param1 param2'
				
  • Append a file containing CR/LF to the end of the file containing the output you want to display.
    1. Using a text editor such as Notepad, create a file named Cr.txt. Press the ENTER key once in the text editor, then save the file.
    2. Change the xp_cmdshell command to redirect the output of the command to a file:
        exec master..xp_cmdshell 'someapp.exe param1 param2 > c:\outfile.txt'
            
      						
    3. Using xp_cmdshell, append the Cr.txt file to the end of your output file:
        exec master..xp_cmdshell 'copy c:\outfile.txt + c:\cr.txt c:\outfile.txt'
            
      						
    4. Use the type command to display the modified output file:
        exec master..xp_cmdshell 'type c:\outfile.txt'
            
      						
  • Pipe the output of your application through another simple application that appends a CR/LF to the end of the stdout stream:
      exec master..xp_cmdshell 'someapp.exe param1 param2 | addcrlf.exe'
    						
    Below is an outline of such an application.

    Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
    #include "string.h"
    #include "stdlib.h"
    int main(int argc, char* argv[])
    {
    	int c;
    	while ((c = getc(stdin)) != EOF) 
    		putc (c, stdout);
    	putc ('\n', stdout);
    	return 0;
    }
      
    						

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

The built-in extended stored procedure xp_cmdshell executes a given command string as an operating-system command shell and returns any output as rows of text.

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbBug kbpending KB255749