1

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

edbras
  • 4,145
  • 9
  • 41
  • 78

4 Answers4

4

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.

dbenham
  • 127,446
  • 28
  • 251
  • 390
2

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.

  • Thanks all for the detailed answer, adding the ~ character did the trick and was enough, even with "git status" – edbras Feb 22 '12 at 22:51
1

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.

macduff
  • 4,655
  • 18
  • 29
0

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.

Aaron
  • 55,518
  • 11
  • 116
  • 132
  • I feel compelled to mention that in light of what the others have posted for answers here; my way is definitely the "long" way. Doing a CALL %* or a CALL %~1 should do what you want. – Aaron Feb 22 '12 at 15:05