windows subcommand evaluation
Asked Answered
O

2

12

linux & unix shells can process subcommands like this.

$> command `subcommand`

does windows cmd shell similar feature?

Orran answered 4/8, 2010 at 18:29 Comment(0)
C
10

You can get something similar using a variation of the for command:

FOR /F "usebackq tokens=*" %%a IN (`subcommand`) DO @command %%a

One big difference (besides being unintuitive and much uglier) is that it'll execute command once for each line produced by subcommand

Note that inside a script file, the percent signs must be doubled up (use non-doubled percent signs if you have some reason to want to do this at the command line).

Caesarism answered 4/8, 2010 at 18:33 Comment(1)
The usebackq isn't really necessary (you can use single-quotes instead): for /f "tokens=*" %f in ('subcommand') do @(command "%f") (I like to use parenthesis with condition statements in 'batch' so you can easily add multiple statements to the "body")Maureenmaureene
M
4

As a whole, no. The Windows Command Prompt really isn't much of a shell in that it doesn't provide much functionality of its own, independent from the commands that you can run. For example, in most Un*x shells, file globbing (matching foo.* to a list of files) is handled by the shell itself. In Windows, every application sees the foo.* from the command line and has to implement the file globbing on its own.

If you're moving down the road of trying to automate Windows, or want a more full-featured shell, you should consider using PowerShell, which does let you do sub-commands:

command (subcommand)

Mamiemamma answered 4/8, 2010 at 18:45 Comment(3)
PowerShell has a lot of other missing features though, some of which cmd.exe does support. For example, &&, ||, <, sensible stderr support, etc... I tried switching, but ultimately gave up on it. You can't be happy with a "native" shell in Windows. Install a UNIX-like shell (Cygwin or MSYS), or better yet, run a GNU/Linux VM. ^_^Maureenmaureene
One of the trickiest things about PowerShell (to me) was the new syntax... it's quite different from the DOS shell, or bash/tcsh, or perl. To your points in particular, PowerShell has "-and" and "-or" rather than "&&" or "||", and actually handles redirection really well. See "help about_logical_operator" or "help about_redirection" for all the details.Mamiemamma
Thanks for the comment. I'll have to look into "-and" and "-or". When I last used it I had done my best to search and all sources seemed to say that it wasn't supported yet (but was on the list). Maybe they implemented those "recently"? In any case, it's ****ing retarded to use what looks like a command option instead of special characters... As for redirection, IMHO I think it's needlessly complex to use objects instead of text. It might make certain process interfaces more efficient, but at the cost of maintaining extra interfaces. The complexity is needless.Maureenmaureene

© 2022 - 2024 — McMap. All rights reserved.