run a input parameter as command in dos batch script?

浪尽此生 提交于 2020-01-05 04:16:06

问题


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".


回答1:


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 for git 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.




回答2:


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'" produces call "echo 'hello'". Try running that from the command line - it doesn't work. You want call echo 'hello'.

One fix would be to change your script slightly: The ~ modifier strips enclosing quotes from the argument

@echo off
call %~1

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

@echo off
%~1

If there are no other arguments on the command line, you might be better off using %* which expands to all the arguments

@echo off
%*
REM or call %*

Now you can call your batch like so

blah.bat echo "hello"

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.




回答3:


I think you'll need %1% to echo the parameters. Here's my lame script which I think does what you want, works with your echo test:

    bla echo hello

Gives:

    C:\tmp>echo bla
    bla

    C:\tmp>echo echo
    echo

    C:\tmp>CALL echo hello
    hello        

    echo %0%
    echo %1%
    CALL %*

If you want to parse through the command line arguments, let me know.




回答4:


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:

SETLOCAL ENABLEDELAYEDEXPANSION

SET VAR1=

FOR %%A IN (%*) DO (
  SET VAR1=!VAR1! %%A
)

call %VAR1%

ENDLOCAL

Then, run your bla.bat without the quotes around git status.

bla 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.



来源:https://stackoverflow.com/questions/9396311/run-a-input-parameter-as-command-in-dos-batch-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!