6

How can I, in Windows, use the output of a script/command as an argument for another script? (A pipe | will not work here, since that other script doesn't read from the standard input)

Too clarify: I have AnotherScript that needs an argument arg, e.g.:

AnotherScript 12

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

AnotherScript (ScriptB)

The AnotherScript 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 is something like:

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

Thanks for the answers. However, given the laborious 'for' construct required, I've rewritten AnotherScript to read from the standard input. That seems like a better solution.

1
  • It'd help us if you tell us what the 'other script' does read from then, if not the standard input. Commented Sep 15, 2009 at 8:07

4 Answers 4

8

Note: All this requires the commands to be in a batch file. Hence the 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 that output in another command:

AnotherScript %args%

This will cause %args% to contain the last line from ScriptB's output, though. If it only returns a single line you can roll this into one line:

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

When used outside a batch file you have to use %x instead of %%x.

However, if ScriptB returns more than one line, AnotherScript runs for each of those lines. You can circumvent this—though only within a batch file—by breaking after the first loop iteration:

for /f "usebackq delims=" %%x in (`ScriptB`) do AnotherScript %%x & goto :eof
Sign up to request clarification or add additional context in comments.

2 Comments

I was about to post the same answer. You beat me! :)
For non-batch-file usage, and if you DO want the second script to run on every line, the following works cmd /v:on then for /f "delims=" %x in ('findstr string files_eg*.csv') do ( set y=%x & set z=!y::=,! & echo !z!) >> results.txt (does substitution of colons with commas)
3

You could save the output of your first script to an environment variable and the use its content as command line parameter to the second script, if that is what you want.

Something like this:

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

Comments

1

Save the data into a temporary file?

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

or call the other script from the first one and transfer the data via command arguments

Comments

0

Aren't you making things way more complicated than necessary?

Call AnotherScript from bash. Where necessary, install Cygwin just so you can do this.

2 Comments

The problem is (was) that AnotherScript needs an argument, and that this argument is the return value of scriptB.
scriptB | xargs AnotherScript

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.