在dos批处理脚本中将输入参数作为命令运行?
如何通过输入参数在 dos 脚本中运行命令?
简化脚本bla.bat:
CALL %1
调用它:
bla.bat“echo 'hello'”(或 bla.bat“git status”)
错误:
“git status”不被识别为内部或外部命令, 可运行的程序或批处理文件。
如果我执行“CALL git status”,它就会起作用。
How can I run a command in my dos script from an input argument ?
Simplified script bla.bat:
CALL %1
Call it:
bla.bat "echo 'hello'" (or bla.bat "git status")
Error:
'"git status"' is not recognized as an internal or external command,
operable program or batch file.
It works if I do "CALL git status".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要记住的重要一点是,扩展的文本必须看起来与您只是从命令行键入命令时的样子完全相同。 (实际上有一些例外,但这是一个很好的起点)。
要调试脚本,只需在调用之前添加 echo:
@echo call %1
。现在尝试像之前那样运行:blah.bat "echo 'hello'"
生成call "echo 'hello'"
。尝试从命令行运行它 - 它不起作用。您想要call echo 'hello'
。一个解决方法是稍微更改您的脚本:
~
修饰符从参数中去掉引号或者您可以放弃调用并简单地使用以下内容(只要您不调用另一个批次)文件)
如果命令行上没有其他参数,您最好使用
%*
,它扩展到所有参数批处理
现在您可以像这样调用 该批次具有各种特殊情况的怪异,可能需要需要解决额外或不同的编码。太多了,无法一一列出——只是期待意想不到的事情。
The important thing to remember is that the expanded text must look exactly like it would if you were to simply key in the command from the command line. (actually there are a few exceptions, but this is a good starting point).
To debug your script, simply put an echo before your call:
@echo call %1
. Now try running as you did earlier:blah.bat "echo 'hello'"
producescall "echo 'hello'"
. Try running that from the command line - it doesn't work. You wantcall echo 'hello'
.One fix would be to change your script slightly: The
~
modifier strips enclosing quotes from the argumentOr you might be able to ditch the call and simply use the following (as long as you are not calling another batch file from which you want to return)
If there are no other arguments on the command line, you might be better off using
%*
which expands to all the argumentsNow you can call your batch like so
Be aware that batch has all kinds of special case weirdness that will likely require extra or different coding to work around. Too many to list - just expect the unexpected.
看来问题可能在于您的输入中有引号,您需要阻止它被分解为不同的 %n 参数,但请尝试:
%~1
这将删除任何输入中的引号。%~$PATH:1
它将去除所有周围的引号,然后在 $PATH env-var 中搜索第一个匹配项,然后扩展字符串以包含命令的完整路径,这不会使用 Windows 发行版为 git 工作,因为 git 是一个批处理文件,cmd 会查找 git status.bat如果要与 git 一起使用,您也可以使用 %~1 和 %~2调用 git 然后将参数提供给git 批处理文件,或者通过修改 $PATH 直接调用 git.exe。但请记住,git.bat 在调用 git 本身之前会进行一些自己的环境设置。
It looks like the problem may be that you have surrounding quotes in your input, which you'll need to stop it being broken into the different %n arguments, but try:
%~1
Which will strip any surrounding quotes from the input.%~$PATH:1
which will strip any surrounding quotes then search within the $PATH env-var for the first match, then expand the string to include the full path to the command, which won't work for git using the windows distro because git is a batch file, and cmd would look forgit status.bat
If its to be used with git, you may as well use %~1 and %~2 to call git then provide the argument to the git batch file, or call git.exe directly by modifying your $PATH. But remember that git.bat does some enviroment setup of its own before calling git itself.
我认为您需要
%1%
来回显参数。这是我蹩脚的脚本,我认为它可以满足您的需求,可以与您的echo
测试一起使用:给出:
如果您想解析命令行参数,请告诉我。
I think you'll need
%1%
to echo the parameters. Here's my lame script which I think does what you want, works with yourecho
test:Gives:
If you want to parse through the command line arguments, let me know.
问题是参数之间的空格让你感到困惑(这就是你在 git status 周围使用引号的原因)。
修改 bla.bat 以迭代命令行参数。这对我有用:
然后,运行 bla.bat,不带 git status 的引号。
本质上,它的作用是迭代命令行参数,然后将它们作为一个命令执行。挑战来自 DOS 中的 FOR 循环,不允许您使用在其内部设置的变量,因此您需要启用变量的“延迟扩展”。然后,您设置的变量需要封装在感叹号(而不是%)中。当然,!VAR1! 之间的空格%%A 阻止参数在 CALL 中一起运行。
The problem is the spaces between the parameters are throwing you off (which is why you were using the quotes around git status).
Modify your bla.bat to iterate through your command line paremeters. This works for me:
Then, run your bla.bat without the quotes around git status.
Essentially, what this does is iterate through your command line parameters, and then executes them all as one command. The challenge comes from FOR loops in DOS not allowing you to use a variable that you're setting within itself, so you need to enable "delayed expansion" of variables. Then, the variable that you're setting needs to be encapsulated in exclamation points (not %'s). And of course, the space between !VAR1! and %%A keeps the parameters from running together in the CALL.