Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows批处理脚本,用于将标准输出重定向到EXE we';我刚跑了_Windows_Batch File_Pipe - Fatal编程技术网

Windows批处理脚本,用于将标准输出重定向到EXE we';我刚跑了

Windows批处理脚本,用于将标准输出重定向到EXE we';我刚跑了,windows,batch-file,pipe,Windows,Batch File,Pipe,我知道在批处理脚本中运行EXE时,批处理脚本的正常行为是等待EXE退出,然后再继续,但是有没有办法让批处理脚本继续执行,但将其标准输出重定向到EXE的标准输入 基本上,我在尝试实现这个巧妙的技巧或类似的东西 @ECHO OFF echo This is a windows batch script... dir /p C:\ C:\cygwin\bash.exe <--- Do some magic here echo This is a bash shell script... ls

我知道在批处理脚本中运行EXE时,批处理脚本的正常行为是等待EXE退出,然后再继续,但是有没有办法让批处理脚本继续执行,但将其标准输出重定向到EXE的标准输入

基本上,我在尝试实现这个巧妙的技巧或类似的东西

@ECHO OFF
echo This is a windows batch script...
dir /p C:\

C:\cygwin\bash.exe <--- Do some magic here

echo This is a bash shell script...
ls -la /cygdrive/c/
exit

echo We're back to the windows batch script again
REM Note: being able to return to the batch script isn't important
@ECHO关闭
这是一个windows批处理脚本。。。
署长/行政长官:\
C:\cygwin\bash.exe这应该可以:

@ECHO OFF
echo This is a windows batch script...
dir /p C:\

(
ECHO echo This is a bash shell script...
ECHO ls -la /cygdrive/c/
ECHO exit
) | C:\cygwin\bash.exe

echo We're back to the windows batch script again
编辑:回复评论

如果不想向每个bash行添加
ECHO
,可以使用以下方法:

@ECHO OFF
echo This is a windows batch script...
dir /p C:\

rem Get the number of the first line in this file that start with "###"
for /F "delims=:" %%a in ('findstr /N "^###" "%~F0"') do set "line=%%a" & goto break
:break

rem Pass from that line to end of this file to bash.exe
more +%line% "%~F0" | C:\cygwin\bash.exe 

echo We're back to the windows batch script again
goto :EOF


#################################################################
# The remainder of this file is a bash script running in cygwin #
#################################################################

echo This is a bash script!!
ls -la /cygdrive/c/
编辑#2

我借用了SonicAtom的方法,并对其进行了轻微修改,以使其更简单。这是:

@ECHO OFF
setlocal

rem This is a magic trick to run the bottom half of this script as a bash script
if not defined _restarted (
    set _restarted=true
    cmd.exe /Q /D < "%~F0"
    rem Put any cleanup commands here
    echo/
    echo Bash script finished
    pause
    exit /B
)
cls
"C:\cygwin\bin\bash.exe"

#################################################################
# The remainder of this file is a bash script running in cygwin #
#################################################################

echo This is a bash script!!
ls -la /cygdrive/c/
@ECHO关闭
setlocal
rem这是一个将此脚本的下半部分作为bash脚本运行的魔术
如果未定义,则重新启动(
设置_=true
cmd.exe/Q/D<“%~F0”
rem在此处放置任何清理命令
回音/
EchoBash脚本已完成
暂停
退出/B
)
cls
“C:\cygwin\bin\bash.exe”
#################################################################
#该文件的其余部分是在cygwin中运行的bash脚本#
#################################################################
这是一个bash脚本!!
ls-la/cygdrive/c/
没有回复吗

我自己找到了答案:

test.bat:

@ECHO OFF

rem This is a magic trick to run the bottom half of this script as a bash script
if not exist _.bat (
    echo @ECHO OFF >_.bat
    echo cmd.exe -c ^< %~nx0 >>_.bat
    _.bat
    rem Put any cleanup commands here
    del _.bat
    echo.
    echo Bash script finished
    pause
    exit /B
)
cls
"C:\cygwin\bin\bash.exe"

#################################################################
# The remainder of this file is a bash script running in cygwin #
#################################################################

echo This is a bash script!!
ls -la /cygdrive/c/
@ECHO关闭
rem这是一个将此脚本的下半部分作为bash脚本运行的魔术
如果不存在uu.bat(
echo@echo OFF>\uu0.bat
echo cmd.exe-c^<%nx0>\uux0.bat
_蝙蝠先生
rem在此处放置任何清理命令
德尔巴特
回声。
EchoBash脚本已完成
暂停
退出/B
)
cls
“C:\cygwin\bin\bash.exe”
#################################################################
#该文件的其余部分是在cygwin中运行的bash脚本#
#################################################################
这是一个bash脚本!!
ls-la/cygdrive/c/
工作原理:


该文件以批处理脚本开始。它编写另一个(临时)批处理脚本,该脚本运行另一个cmd.exe shell,将原始批处理文件作为stdin定向到cmd.exe。原始批处理文件的作用类似于包含键盘上键入的命令的文本文件。它运行bash.exe,并继续向bash提供stdin。唯一的缺点是@ECHO OFF在调用bash.exe之前运行的批处理文件部分无效。这是因为似乎没有办法关闭cmd.exe中的键盘回声。

啊,我们同时发布了一个答案!:)这种方法很有意义,但我不喜欢在每行的开头都附加
ECHO
。我的解决方案稍微复杂一点,但会产生更清晰的代码。。。。还有编辑#2。恐怕您的方法不起作用,原因是:在bash.exe结束执行后,批处理文件将继续执行下一行,因此它会在每个bash行中标记错误。确定。我检查了你的代码,我以前的评论是错误的。但是,您需要通过调用命令执行第二个批处理文件:
CALL\uubat
;否则,控件不会返回到原始文件,清理代码也不会执行。(这是一个多么复杂的方法啊!)事实上,我的方法确实有效,包括清理代码,我知道,因为我正在使用它。但我更喜欢你上面的第一个编辑答案,它更干净、更优雅!谢谢!:)