当应用程序使用stdin从批处理中运行时,如何将标准输入提供给批处理文件?

时间:2022-04-27 02:42:11

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

这是一个最小的批处理文件demo.bat,用于说明我的问题:

@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 so:

我有一个输入文件foo.txt,看起来像这样:

foo_value
bar_value

I run my batch file as demo.bat < foo.txt. The output is:

我将我的批处理文件作为demo.bat 运行。输出是:

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 remove the sqlcmd -?, then bar is "typed" as bar_value, which is what I originally expected.

如果我删除了sqlcmd - ?,那么bar被“键入”为bar_value,这是我最初的预期。

So, it looks to me like sqlcmd is not playing nice somehow with the standard input that wasn't meant for it. Anyone have any bright ideas on how I can work around it? In a perfect world, the solution would not involve changing the original batch file, or involve installing third-party packages to drive the interaction (e.g. Expect).

因此,在我看来,sqlcmd不能以某种方式使用不适合它的标准输入。任何人对如何解决它有任何好主意?在完美的世界中,解决方案不涉及更改原始批处理文件,或涉及安装第三方包以驱动交互(例如Expect)。

2 个解决方案

#1


2  

You can also redirect NUL to sqlcmd:

您还可以将NUL重定向到sqlcmd:

sqlcmd -? < NUL

but this would require changing the batch script as well.

但这也需要更改批处理脚本。

#2


2  

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

我不知道它是否有帮助,但您可以尝试将其他内容传递给sqlcmd,例如:

echo. | sqlcmd -?

#1


2  

You can also redirect NUL to sqlcmd:

您还可以将NUL重定向到sqlcmd:

sqlcmd -? < NUL

but this would require changing the batch script as well.

但这也需要更改批处理脚本。

#2


2  

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

我不知道它是否有帮助,但您可以尝试将其他内容传递给sqlcmd,例如:

echo. | sqlcmd -?