cmd enabledelayedexpansion

Let’s talk about variable delayed expansion first. Of course, if you do a dog search, you can see articles about variable delay expansion flying all over the sky, so I will briefly introduce it here. Let's look at a batch first:

set str=test

if %str%==test (
    set str=another test
    echo %str%
)

  

The above code segment is extremely simple, assign a value to str, determine whether its value is test, if so, reassign it to another test, and then display the value of str.

As a normal person's thinking, it must be another test displayed here, but it is not, it is still displayed, why is this? Because: when windows interprets and executes this code segment, after encountering the parentheses after the if statement, it only treats it as one statement instead of two statements, so the %str% in the second statement will be replaced by Its current value test, the above code is equivalent to the effect of the following code:

set str=test

if %str%==test (
    set str=another test
    echo test :: note here
)

  

Therefore, the output is naturally test.

In this way, the flexibility of programming is greatly reduced. Therefore, M$ thought of a method for the workplace, which is variable delay. It is very simple. See the following code:

@echo off
setlocal enabledelayedexpansion :: note here

set str=test

if %str%==test (
    set str=another test
    echo !str! :: note here
    echo %str% ::difference
)

  

What will be output now? Try it and you will know that the first line outputs another test, and the second line outputs test.

Now explain that it  setlocal enabledelayedexpansion is used to enable variable delay, which is to tell the interpreter that when a compound statement is encountered, it should not be processed as one statement at the same time, but still be interpreted one by one. But at this time, you must use !str! to refer to the variable. If you still use %str%, it will not work.

Okay, variable delayed expansion explained, at least that's what I know about variable delayed expansion.


Variable delay expansion and exclamation marks gave me a lot of pain today, so I'm going to talk about them now.

Try the following code snippet:

@echo off

setlocal enabledelayedexpansion

set str=test!!!

echo %str%

  

Turn on the variable delay, assign a value to str, and output str, so the output is naturally test!!!. But in fact, windows tells us that it is wrong again, the output is test, and the exclamation mark is eaten by windows.

Of course, if you comment out the line that enables variable delay, these lines can work normally, so it is said that enabling variable delay affects our normal work, but I still haven't figured out why this happens, only M$ knows.

So, turn off the variable delay, but the program needs to use the variable delay to implement some logic, what should I do? Two ways:

  1. Temporarily turn off variable delay:

    @echo off
    
    setlocal enabledelayedexpansion
    
    :: do something here
    
    setlocal disabledelayedexpansion :: disable variable delay
    
    set str=test!!!
    
    echo %str%
    
    setlocal enabledelayedexpansion
    
    :: continue...
    

      

    Where the exclamation mark needs to be processed, temporarily turn off the variable delay, and then turn it on again after processing. At this time, the exclamation mark can be output normally.

  2. Set before turning on the variable delay:

    @echo off
    
    set str=test!!!
    
    setlocal enabledelayedexpansion
    
    echo !str! :: note here
    

      

    Set the variable before enabling the variable delay, but note that when using the variable, you need to use the exclamation mark to quote. At this time, the exclamation mark can also be output normally.


Finally, I posted a piece of code that made me sore (the variable delay is naturally turned on):

set server=%~1
set username=%~2
set password=%~3
echo %date%, %time% [INFO]   Report server address: [%server%], username: [%username%], password: [*******]

rs.exe -i "PublishReports.rss" -s "%server%" -u "%username%" -p "%password%" -l 600

  

In order not to echo password, I output a bunch of *. When testing on my machine, my password characters are all regular, so it passes. However, there is an exclamation mark in the QA password. . .

The key point is that the rs.exe of M$'s Report Server is called here to upload the template. It throws an exception of Could not connect, so I naturally thought that there was a problem with the service of the report server, but I checked it for a long time. There is no problem with the report server. . . Who TMD will think that there is an exclamation mark in someone else's password, who will think that it is caused by the conflict with the so-called delay variable, and who will think that I am clever enough to display a large string of hard codes in order not to display the password. The asterisk prevents the value of password from being seen. . .

Original link:

https://kelvinh.github.io/blog/2012/02/16/batch-delayed-expansion/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325208696&siteId=291194637
cmd