简体   繁体   中英

How To Use A Command Output As An Argument of Another Command?

This is an update for an old question of mine, because I wasn't so clear in it.
Honestly I didn't even have the Win_CLI experience to ask the right question. So I'm sorry, and I wish this time is better.

Like I said in my question, I need to use a command output as an argument of another command.

For Example:-

CLITool -Switch (Value_OF_a_Command_OutPut)

I think what I need here is the Set command:-

Set Value=
         Command

CLITool -Switch %Value%

But, the problem is that the output value will be printed with a Line_Break!

OutPut_Value
CLITool -Switch (Nothing!)

and because of this, the command won't be executed correctly.

So.. my question now, is how to fix that Set Command problem,
and If possible, how to do the same thing in another technique?

BTW..As most of Linux users know, it's possible to use a command output in the same commandline of the other command as follow:

CLITool -Switch $(Command)

I wish if there is something similar in windows CLI.

Please tell me if it's better to post my actual command.


Thanks Guys,
Appreciate Your Help :)

You can use a for /f the command. For each line in the output of the executed command, the code in the do clause will be executed, with the content of the line stored in the for replaceable parameter

 for /f "delims=" %%a in ('command') do CLITool -Switch %%a

Also, you can execute the command, send the output to a temporary file and retrieve its contents with a input redirection and a set /p command

command > "tempfile"
<"tempFile" set /p "var="
CLITool -Switch %var%

In both cases, under the assumption that command will echo only one line. If it generates more lines, the for will execute CLITool for each of the lines, while the redirection option will only retrieve the first line from the temp file.

The third way to solve it is to use a pipe. The left side of the pipe generates the content and the right side reads this content.

command | ( set /p "var=" && @call CLITool -Switch %%var%% )

Why the call and the %%var%% syntax? We are setting a variable inside a line and in the same line we need to retrieve the variable and for this to work, delayed expansion is needed. But by default delayed expansion is disabled and enabling it does not solve anything as each side of the pipe is executed inside its own cmd instance that is started without delayed expansion (default behaviour) so, the trick is to escape the reference to the variable (the reason for the doubled percent sign) and force a double parse phase in the line (the reason for the call )

The alternative could be something as

command | cmd /e /v /q /c "set /p "var=" && CLITool -Switch !var!"

This way we are running in the right side of the pipt a cmd instance with delayed expansion enabled ( /v ), extensions enabled ( /e ), with echo off ( /q ) that will execute the needed command.

But the pipe code (in both cases) has a drawback: the variable is set in a separate cmd instance, so, your main batch file will no have access to its value.

If You have two lines being returned from an executable and wish to assign each to a variable, then

set "line1="&set "line2="
for /f "delims=" %%a in ('yourcommand') do if defined line1 (set "line2=%%a") else (set "line1=%%a")
set line

should save and display the results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM