Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

Why I can't CALL "IF" and "FOR" neither in batch nor in the cmd?

To be honest I don't expect a satisfying answer here.These command cannot be called and that's it (as far as I know the only commands that cannot be used with call). Here are few examples with their output:

C:Windowssystem32>call @if a==a echo called 
',' is not recognized as an internal or external command,
operable program or batch file.

C:Windowssystem32>call (@if a==a echo called)
';' is not recognized as an internal or external command,
operable program or batch file.

C:Windowssystem32>call if a==a echo called
'if' is not recognized as an internal or external command,
operable program or batch file.

C:Windowssystem32>call for  %a in (1) do @echo called
'for' is not recognized as an internal or external command,
operable program or batch file.

C:Windowssystem32>call @for  %a in (1) do @echo called
'+' is not recognized as an internal or external command,
operable program or batch file.

C:Windowssystem32>call (@for  %a in (1) do @echo called)
';' is not recognized as an internal or external command,
operable program or batch file.

I have some suspicious , that IF and FOR are not in fact 'real' commands - they just decide to pass the command prompt control to the line (or brackets block) behind them or not , which confuses the call. Anyway even that , when there are syntax errors in called FOR or IF they are detected:

C:Windowssystem32>call (@if a= echo called)
= was unexpected at this time.

C:Windowssystem32>call (for %a (1) do echo called)
(1 was unexpected at this time.

So at least the parsing is done.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are quite right.

FOR, IF and REM aren't normal internal commands (but for REM exists also a normal internal version).
They use an own special parser (each a different one).

It seems that these commands are translated to a sort of token.
Therefor you got such unexpected error messages (there are much more possible characters), the error/token character depends also of the Windows version (if I remember correct here).
Probably the CALL command sees only the tokenized data from the original command.

Command blocks don't work at all with the call command, also &|<> don't work as expected.

call (a^|a)

Searches for a command/token 2, so if you create a batch file named 2.bat you can start it with call (a^|a).

For further information about CALL
Dostips:CALL me, or better avoid call
Dostips:Limit CMD processing to internal commands, safer and faster?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...