Run input parameter as command in dos batch script?

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, operating program, or batch file.

It works if I do "CALL git status".

+1
source share
4 answers

It seems like the problem may be that you have surrounding quotation marks at your input, that you need to stop it from being split into different% n arguments, but try:

%~1 Which will strip out any surrounding quotation marks from input.

%~$PATH:1 , which will separate any surrounding quotes, then search within $ PATH env-var for the first match, then expand the line to include the full path to the command that will not work for git using windows distro, because that git is a batch file and cmd will look for git status.bat

If used with git, you can use% ~ 1 and% ~ 2 to call git, then provide an argument in the git batch file or call git.exe directly by changing your $ PATH. But remember that git.bat does some customization of its own environment before invoking git.

+2
source

It is important to remember that extended text should look exactly as if you were just using a command from the command line. (there are actually a few exceptions, but this is a good starting point).

To debug your script, simply echo it before your call: @echo call %1 . Now try to run as you did before: blah.bat "echo 'hello'" creates a call "echo 'hello'" . Try to run this from the command line - this will not work. You want call echo 'hello' .

One fix would be to slightly modify your script: the ~ modifier separates the quotation marks from the argument

 @echo off call %~1 

Or you can skip the call and just use the following (unless you call 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 will expand to all arguments

 @echo off %* REM or call %* 

Now you can challenge your party like this

 blah.bat echo "hello" 

Keep in mind that the party has all kinds of special cases that are likely to require additional or different coding to work. Too many lists - just expect the unexpected.

+3
source

I think you will need %1% to repeat the parameters. Here's my lame script, which I think does what you want, works with its 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 the command line arguments let me know.

+1
source

The problem is that spaces between options throw you away (which is why you used quotes around git state).

Modify your bla.bat to iterate using command line parameters. 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 quotes around the git state.

 bla git status 

Essentially, this is that it iterates through the command line options, and then executes them all as one command. The problem arises from FOR loops in DOS that prevent you from using the variable that you set inside yourself, so you need to enable the "slow expansion" of the variables. Then the variable you set should be encapsulated in exclamation points (not%). And of course, the space between! VAR1! and %% A makes the parameters work together in CALL.

0
source

Source: https://habr.com/ru/post/1401648/


All Articles