Why CALL prints the GOTO help message in this script?And why command after that are executed twice?

后端 未结 2 811
难免孤独
难免孤独 2021-01-02 13:22

Here\'s one interesting thread. And I tried to play with the two things discussed there.

  1. You can access labels with special symbols with double expansion.
2条回答
  •  醉梦人生
    2021-01-02 13:33

    I'm always surprised, that you still found things that never came to my mind to test.

    Contrary to Aacini, I don't believe that :/? acts here as a valid label.
    Else this should find such a label.

    I suppose that the CALL command is internally composed of a stack pusher function and then just use GOTO to jump to the label.

    And as you are using late expansion the /? isn't detected by the CALL command itself, but by the GOTO command.
    The goto shows only the help info and finished, but as the call has already pushed the fileposition to the stack it works like calling this filepostion and later return to the same location!

    set "help=^ /?"
    call :myLabel%%help%%
    

    This shows also the help, like a GOTO :myLabel /? would do.
    But this one don't

    set "help=/?"
    call :myLabel %%help%%
    

    I suppose, the GOTO gets only the label parsed by the CALL, the other parameters are moved to %1, %2, ...

    And this could explain why only the label can use delayed expansion two times.

    @echo off
    setlocal EnableDelayedExpansion
    set "label=myLabel"
    set "pointer=^!label^!"
    call :!pointer!
    exit /b
    
    :myLabel
    echo it works
    

提交回复
热议问题