Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

batch-file

cmd

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. Labels that contain /? cannot be used because GOTO and CALL prints their help messages instead of execution.

And here's the result:

@echo off

setlocal enableDelayedExpansion
set "label=/?"

    call :%%label%%
    echo == Test message to check if the CALL or GOTO ( or neither ) command is executed  ==

exit /b 0

:/?
    echo == CALL or GOTO has been executed ==
exit /b 0

And the output:

Directs cmd.exe to a labeled line in a batch program.

GOTO label

  label   Specifies a text string used in the batch program as a label.

You type a label on a line by itself, beginning with a colon.

If Command Extensions are enabled GOTO changes as follows:

GOTO command now accepts a target label of :EOF which transfers control
to the end of the current batch script file.  This is an easy way to
exit a batch script file without defining a label.  Type CALL /?  for a
description of extensions to the CALL command that make this feature
useful.
== Test message to check if the CALL or GOTO ( or neither ) command is executed  ==
== Test message to check if the CALL or GOTO ( or neither ) command is executed  ==

And the code after the CALL is executed twice??

EDIT

This is even more unexplainable to me:

@echo off

setlocal enableDelayedExpansion
set "label=/?"
    set /a x=1
    call :%%label%% >nul

    set /a x=x+1
    echo ---
    echo -%x%-
    echo ---

exit /b 0

:/?
    echo == CALL or GOTO has been executed ==
    echo == first argument : %1 ==
exit /b 0 

The output is:

---
-3-
---

The code after the call of the CALL for sure is executed twice, but the output of the first run can be redirected in the same line?

like image 945
npocmaka Avatar asked Aug 13 '15 11:08

npocmaka


People also ask

What does goto command do?

GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control.

How do I stop command prompt from closing after running?

If you want the command prompt cmd widnow to stay open after executing the last command in batch file –you should write cmd /k command at the end of your batch file. This command will prevent the command prompt window from closing and you'll get the prompt back for giving more commands in the cmd window.

What is @echo off in batch script?

batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.

What is %% A in batch script?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


2 Answers

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
like image 161
jeb Avatar answered Oct 21 '22 13:10

jeb


Interesting! The first code may be reduced to:

@echo off

setlocal
set "label=/?"

    call :%%label%%
    echo == Test message to check if the CALL or GOTO ( or neither ) command is executed  ==

and still show the same output, that is, it behaves the same way than this code:

@echo off

setlocal

call :/?
echo == Test message to check if the CALL or GOTO ( or neither ) command is executed  ==

where the call :/? is both a call command and a valid label, AND considering :/? a valid label. Wow!

Why the GOTO help is displayed? No idea!!!

EDIT: Additional tests

This code:

@echo off

setlocal
set "label=/?"

    set i=0
    call :%%label%%  &  echo Command in same line:  i=%i%
    set /A i+=10
    echo == Test message ==     i=%i%

show the GOTO help screen first, and then:

== Test message ==     i=10
Command in same line:  i=0
== Test message ==     i=20

but if EnableDelayedExpansion is added and %i% is changed by !i! in the call line, then it shows:

== Test message ==     i=10
Command in same line:  i=10
== Test message ==     i=20

as "expected"...

EDIT #2

The test below show that the call :%%label%% command does NOT report the ERRORLEVEL=1 standard on "label not found" error:

@echo off

setlocal
set "label=/?"

    call :notFound
    echo Label not found: ERRROLEVEL = %ERRORLEVEL%
    ver > NUL
    echo Reset errorlevel: ERRROLEVEL = %ERRORLEVEL%

    call :%%label%%
    echo == Test message == ERRROLEVEL = %ERRORLEVEL%
like image 21
Aacini Avatar answered Oct 21 '22 13:10

Aacini