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

KorkOoO

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 :)

MC ND

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to pass the output of one command as the command-line argument to another?

From Dev

How to use the previous command output to use as a part of another command: python

From Dev

How to use output file in another powershell command?

From Dev

How to pass a terminal command as an argument to another command

From Dev

How to specify a compound command as an argument to another command?

From Dev

How can I use the output of an external command as an argument to tabnew?

From Dev

How to use output for another action in one command line?

From Dev

How to use output of a command inside an awk command?

From Dev

How to divide the output of one command by the output of another?

From Dev

How to pass the output of a command as argument in gdb?

From Dev

How to pass the output of previous command to next as an argument

From Dev

How to pass the output of a script as an argument to a ssh command?

From Dev

How to use output directly as a command

From Dev

How do I pipe the output of a curl command to an environment variable and use it in another curl command?

From Dev

Win10 - How to pipe the output from one command as an argument of another to check file hash?

From Dev

How to use the Powershell command argument in a SendTo shortcut

From Dev

How to pass output as arguments to another command?

From Dev

How to move output of find command to another directory?

From Dev

How to use knitr from command line with Rscript and command line argument?

From Dev

Use a parameter in a command argument

From Dev

How to pass command output as multiple arguments to another command

From Dev

How to pipe a single item from a command output into another command?

From Dev

How to start line with command from output of another command

From Dev

How to pipe output of command to another command while also displaying it on the console?

From Dev

How to pipe single result from output of locate command to another command?

From Dev

Is it possible to use the output of bundle show command as an argument of cd?

From Dev

Use find results as command line argument for another process

From Dev

bash script needs to read user input and use it as the argument for another command

Related Related

  1. 1

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

  2. 2

    How to pass the output of one command as the command-line argument to another?

  3. 3

    How to use the previous command output to use as a part of another command: python

  4. 4

    How to use output file in another powershell command?

  5. 5

    How to pass a terminal command as an argument to another command

  6. 6

    How to specify a compound command as an argument to another command?

  7. 7

    How can I use the output of an external command as an argument to tabnew?

  8. 8

    How to use output for another action in one command line?

  9. 9

    How to use output of a command inside an awk command?

  10. 10

    How to divide the output of one command by the output of another?

  11. 11

    How to pass the output of a command as argument in gdb?

  12. 12

    How to pass the output of previous command to next as an argument

  13. 13

    How to pass the output of a script as an argument to a ssh command?

  14. 14

    How to use output directly as a command

  15. 15

    How do I pipe the output of a curl command to an environment variable and use it in another curl command?

  16. 16

    Win10 - How to pipe the output from one command as an argument of another to check file hash?

  17. 17

    How to use the Powershell command argument in a SendTo shortcut

  18. 18

    How to pass output as arguments to another command?

  19. 19

    How to move output of find command to another directory?

  20. 20

    How to use knitr from command line with Rscript and command line argument?

  21. 21

    Use a parameter in a command argument

  22. 22

    How to pass command output as multiple arguments to another command

  23. 23

    How to pipe a single item from a command output into another command?

  24. 24

    How to start line with command from output of another command

  25. 25

    How to pipe output of command to another command while also displaying it on the console?

  26. 26

    How to pipe single result from output of locate command to another command?

  27. 27

    Is it possible to use the output of bundle show command as an argument of cd?

  28. 28

    Use find results as command line argument for another process

  29. 29

    bash script needs to read user input and use it as the argument for another command

HotTag

Archive