Why is my piping failing?

亡梦爱人 提交于 2020-01-16 02:12:10

问题


I've been messing around with .cmd scripts, and wanted to practice piping. I wrote one script to make files, and another to edit them with Notepad++. The making script (called create.cmd) is as follows:

@echo off
copy nul %1 > nul
echo %1

And the edit script (called edit.cmd) is as follows:

@echo off
start notepad++.exe %1

Now, I wanted to try and make a file, and then pipe its output (hence the echo line) in the form of the name of the file to the edit script. So what I wrote was this:

create foo.txt | edit

However, this fails - I get an open Notepad++ window, but my newly-created file does not appear there. What am I missing or doing wrong here?


回答1:


You are not reading from the pipe in your second batch file.

For reading just one line of output from the first batch, the filename, this should suffice:

@echo off
set /p file=
start notepad.exe %file%

Otherwise check Read stdin stream in a batch file for reading multi-lined input.




回答2:


edit.bat has no %1 parameter

You could try this:

@echo off
copy nul %1 > nul
echo %1
call edit %1


来源:https://stackoverflow.com/questions/21493191/why-is-my-piping-failing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!