Checking input command in dos? Batch file

I run the input arguments as a command in the dos dos file, as I asked in: Run the input parameter as a command in the dos batch script . This works, however the problem I am experiencing is: checking if the input command, if it is empty. What am I doing:

SETLOCAL set CMD=%* echo Running command [%CMD%] IF "%CMD%"=="" ( echo "Input argument missing: command" GOTO :end ) echo "XXX %CMD%" 

And working:

 script.bat echo "abc" 

It will display:

 Running command [echo "abc"] b was unexpected at this time. 

This happens in the IF condition, but why?

How to check if my input is empty. I know how to check if a string is empty in dos, but for some reason it does not work in this combination :(
BTW: If I remove the IF condition, it works well, that is: the command is executed correctly.

  • Ed
+4
source share
1 answer

First check for missing parameters, then assign and process the command:

 IF .%1 == . ( echo "Input argument missing: command" GOTO :eof ) SETLOCAL set CMD=%* echo Running command [%CMD%] echo "XXX %CMD%" 

As for “Why?”, This is because the quotation mark in the CMD value (before a ) closes the quotation mark before %CMD% , and therefore the string appears as "echo "abc"" , and thus b becomes separated from the string, which crashes the command processor. (He expects a comparison token, e.g. == or EQU , but not some weird b ).

+3
source

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


All Articles