Redirecting output to a command line argument of another command in Windows

How can I use the output of a script / command on Windows as an argument for another script? (The pipe | does not work here, since this other script cannot be read from standard input)

Too clear: I have AnotherScript that needs an arg argument, for example:

AnotherScript 12 

now I want the argument (12 in the example) to come from the output of the script, calls it ScriptB. So I would like something like strings

 AnotherScript (ScriptB) 

The other script is actually a python script that requires an argument, and ScriptB is a cygwin bash script that produces some number, so the way I would like to use it looks something like this:

 c:\Python26\python.exe AnotherScript (c:\cygwin|bin|bash --login -i ./ScriptB) 

Thanks for answers. However, given the time-consuming "for" design, I rewrote AnotherScript to read from standard input. This seems like the best solution.

+5
source share
4 answers

Note. All this requires that the commands are in a batch file. Therefore, double signs % .

You can use the for command to capture the output of the command:

 for /f "usebackq delims=" %%x in (`ScriptB`) do set args=%%x 

then you can use this output in another command:

 AnotherScript %args% 

This will cause %args% to contain the last line from the output of ScriptB . If it returns only one row, you can flip it into one row:

 for /f "usebackq delims=" %%x in (`ScriptB`) do AnotherScript %%x 

When used outside of a batch file, you should use %x instead of %%x .

However, if ScriptB returns more than one line, AnotherScript is executed for each of these lines. You can get around this, although only in a batch file, by splitting it after the first iteration of the loop:

 for /f "usebackq delims=" %%x in (`ScriptB`) do AnotherScript %%x & goto :eof 
+8
source

You can save the output of the first script to an environment variable and use its contents as a command line parameter for the second script, if that is what you want.

Something like that:

 for /f "delims=" %a in ('first_script.cmd') do @set ouput=%a second_script %ouput% 
+3
source

Save data in a temporary file?

or use a named pipe instead of a pipe on standard input and output.

or call another script from the first and pass the data through the command arguments

+1
source

Don't you make things more complicated than necessary?

Call AnotherScript from bash. If necessary, install Cygwin so that you can do this.

0
source

All Articles