In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about the usefulness of the ECHO command in batch processing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
As we all know, if echo is followed by an environment variable, but the variable is empty, it is equivalent to echo without any parameters, that is, whether the output current echo is on or off. The solution given in many articles or tutorials is to add a dot echo., after echo so that blank lines are output.
The code is as follows:
@ echo off
Echo demon.tw%
:: ECHO is off.
Echo.%demon.tw%
Pause as far as I know, there are at least ten ways to output blank lines with echo:
The code is as follows:
@ echo off
Echo=
Echo
Echo
Echo+
Echo/
Echo [
Echo]
Echo:
Echo.
Echo\
Pause
The ten methods can be divided into three groups, and the efficiency of each group decreases in turn. Sadly, the tutorials that are considered classic are the echo in the least efficient group.
Echo. It is not only inefficient, but also prone to errors:
The code is as follows:
@ echo off
Cd. > echo
Echo.
Pause
I know it's hard for you to accept, but it is.
In the first group, the =,; after echo are all delimiters in the batch, so CMD can correctly parse the echo command and take the =,; as an argument to the echo command. Yes, you read it correctly, the delimiter is not used to separate commands from parameters, they are usually part of the parameters. Since it is a parameter, why not be output? That's because the echo command directly skips the first character of the parameter, starting with the second character, and the second character is NUL, so it prints a blank line.
You may ask again, why do you use spaces as delimiters but not output blank lines? That is because before the output, CMD wants to check whether the parameter of the echo command is on or off, or if the parameter is empty: first skip all blank characters, and if the string ends after skipping, then it is considered that there is no parameter, and the output echo is on or off;. If the string does not end, call the wcsnicmp function to determine whether the remaining string is on or off, and then modify the state of echo.
So adding a lot of spaces has the same effect:
The code is as follows:
@ echo off
Echo
Echo on
Echo
Pause
For the second and third groups, things are not that simple. Because echo is not followed by a delimiter, it is parsed as a whole, while echo+ echo/ and so on are obviously not internal commands, and CMD searches them as external commands. Well, you know, searches are time-consuming, which is why they are less efficient than the first group.
Unfortunately, CMD makes a lot of effort to search, but still can't find such an external command, so it tries to Fix the command to see if there are any characters in the command (see figure):
As you can see, CMD's treatment of:.\ is not quite the same as + [] /. If it is + [] /, CMD will directly remove them from the command and add them to the front of the original parameters; if it is:.\ and the CMD extension is enabled, then the GetFileAttributes function will be called one more time to obtain file attributes, and it will naturally take more time to call the function, so the efficiency of the third group is slightly lower than that of the second group.
Let's explain why echo. Sometimes it can lead to mistakes. In theory, GetFileAttributes functions should return-1 (INVALID_FILE_ATTRIBUTES), but this is not the case, and I don't know if this is the BUG of the GetFileAttributes function:
The code is as follows:
# include
# include
Int main ()
{
FILE * fp = fopen ("echo", "wb")
Fclose (fp)
Printf ("0x%x\ n", GetFileAttributes ("echo:"))
Printf ("0x%x\ n", GetFileAttributes ("echo."))
Printf ("0x%x\ n", GetFileAttributes ("echo/"))
Return 0
}
If you test the C program above, you will find that echo. That line does not return-1.
If the GetFileAttributes function does not return-1 (generally indicating that the file does not exist) or 0 × 10 (indicating that the file is a folder), then the command will remain the same and run as an external command.
The code is as follows:
@ echo off
Cd. > echo
Echo.
Pause
'echo.' Is not recognized as an internal or external command, operable program or batch file.
The code is as follows:
@ echo off
Cd. > echo
Setlocal disableextensions
Echo.
Pause
Closed the CMD extension, no problem.
The code is as follows:
@ echo off
Md echo
Echo.
Pause
Echo is a folder, not a file, no problem.
Finally, to sum up, in most cases, you should use the first set of echo, echo; echo= for output, they are the same efficiency as echo (spaces), and can be used to output on or off, and can also output blank lines when the variable is empty.
But echo, echo; echo= cannot export to /? The first lines, if you want, can use the second set of echo+ echo/ echo [echo], which are less efficient but can guarantee the output as is.
I don't recommend that you use the third group of echo: echo. Echo, if you still want to use it like in junk tutorials, I can't help it.
Thank you for reading! On "what is the use of ECHO commands in batch processing" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 303
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.