How can I feed standard input to a batch file when an application is launched from batch mucks using stdin?

Here's the minimal batch file, demo.bat , to illustrate my problem:

 @ECHO off set /p foo=Enter foo: echo. echo you typed "%foo%" sqlcmd -? set /p bar=Enter bar: echo. echo you typed "%bar%" 

I have an input file foo.txt that looks like this:

  foo_value
 bar_value

I run my batch file as demo.bat < foo.txt . Exit:

  Enter foo:
 you typed "foo_value"
 Microsoft (R) SQL Server Command Line Tool
 Version 9.00.3042.00 NT INTEL X86
 Copyright (c) Microsoft Corporation.  All rights reserved.

 usage: Sqlcmd [-U login id] [-P password]
   (... etc ...)

 Enter bar:
 you typed "foo_value"

If I delete sqlcmd -? , then the string is "entered" as bar_value , as I expected.

So, it seems to me that sqlcmd does not play so well with standard input, which was not intended for it. Anyone any bright ideas on how I can get around this? In an ideal world, the solution would not be to modify the original batch file or to include the installation of third-party packages for interaction management (for example, Expect).

+3
source share
2 answers

You can also redirect NUL to sqlcmd:

 sqlcmd -? < NUL 

but it will also require modifying the script package.

+2
source

I don't know if this helps, but you can try passing something else to sqlcmd, for example:

 echo. | sqlcmd -? 
+2
source

All Articles