In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what the symbols commonly used in cmd batch processing are, and the editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. @
It is usually followed by a command or statement, then the command or statement itself does not appear on the screen when executed. Save the following code as a test.cmd file, and then run it to compare the difference in the output of the two echo statements on the screen:
Echo a
@ pause
@ echo b
@ pause
The implementation results are as follows:
C:\ Documents and Settings\ JM\ Desktop > echo a
A
Please press any key to continue.
Please press any key to continue.
2.%,%
The percent sign is used on different occasions and has different meanings:
① when the percent sign appears in pairs and contains non-special characters, it usually does variable reference processing, such as% var%,% str%. Save the following code as a batch file and observe the results on the screen after running:
@ echo off
Set str=abc
The value of the echo variable str is:% str%
Pause
The result is displayed on the screen:
The value of the variable str is: abc
Press any key to continue.
In addition, the percent sign has a special form as a variable reference, that is, a reference to a formal parameter. At this point, a single percent sign is followed by 10 numbers such as% 0,% 1, where% 0 is the name of the script itself, and% 1 to% 9 are the second to ninth parameters. A variable reference is supported after% 0 percent% 9 minutes% 10 at most, that is,% 15 is the value of% 1 followed by 5.
Take a look at the demo code:
@ echo off
If defined str goto next
Set str=
Set / p str= Please drag the file to this window and enter:
Call "% ~ 0" str%
Pause
Exit
: next
Cls
Echo the full path of this batch file is: "% ~ 0"
The full path to the file dragged by echo to this window is: "% ~ 1"
Goto: eof
When ② appears in the set / a statement, it represents the division of two numbers by the remainder, which is so-called modular operation, which is written slightly differently in the command line window and in the batch file: in the command line window, only a single% is needed, and in the batch file, two consecutive% signs are required, written as%%.
For example, if you run set / a num=4%2 in a command line window, the result will display 0 because the remainder of 4 divided by 2 is 0; if saved as a batch file, this statement will change slightly:
@ echo off
Set / a num=4%%2
The remainder of echo 4 divided by 2 is% num%
Pause
③ escape symbol: if you want to display% itself, you need to escape with% in front of it. For example:
@ echo off
Echo a% sign:%
Echo two percent signs:%
Three percent signs of echo:%
Pause
3 、: 、::
① starts with: a single: indicates that the line is a tag, and the content after it is a tag segment, such as: test, which means that the content under test is the tag segment, and test is the name of the tag segment. You can jump to the tag segment with goto test, goto: test or call the subprocedure with call: test And two consecutive colons start to indicate that the content of the line is a comment. In fact,:: is an invalid signature.: plus spaces can also play the role of comments. At this time, the function of:: is the same as the comment command rem. However, some command symbols in rem comment statements, such as redirect symbols and pipe symbols, will still be executed, and if you use:: to comment, all commands or symbols on the same line are directly ignored by the command interpreter, which virtually improves the compatibility of comments and the execution efficiency of the whole program, and is more eye-catching in many command statements, so the format of:: is recommended for comment statements.
When ② is used at the same time as: and ~ in the set statement,: plays the function of intercepting strings. Suppose set str=abcde, then set var=%str:~0,1% means to intercept the first character of the string abcde; when used at the same time as =, it functions as a replacement string. Suppose: set str=abc:de, then set var=%str:a=1% means to replace an in the string abc:de with 1 department set var=%str::=2% means to replace the: in the string abc:de with 2
4. ~
① is used in set statements, and: when used at the same time, it has the function of intercepting strings. Please refer to the explanation of the previous item.
When ② is used in a set / a statement, it is a unary operation symbol that denotes the bitwise inversion of the operands. For example, the execution result of set / a num=~1 is-2 and the result of num=~0 is-1.
③ is used in for statements to enhance the function of for so that more information can be extracted. For example, in the for statement of a batch file:% ~ I means to remove the first pair of outer quotation marks,% ~ zi means to get the size of the file (in bytes),% ~ ni means to get the file name,% ~ xi means to get the extension (with dots). They can be used in combination, such as% ~ nxi to get the file name and suffix name.
5, >, >
Generally speaking, > means to overwrite the contents of the original file with new content, > > means to append content to the original file, and at this point, they appear as redirecting symbols; if used in set / a statements, > indicates grouping, and > > indicates logical shift
6. |
Generally speaking, it appears as a pipe symbol, indicating that it takes the execution result of the command or statement before it as the processing object of the command or statement after it. In short, it takes the output before it as the input after it, for example: echo abcd | findstr "b", which means taking the execution result of echo abcd as the execution object of findstr "b", that is, looking for the b character in the string abcd. If there is an abcd string in test.txt, the statement has the same effect as findstr "b" test.txt
7. ^
In general, ^ appears as an escaped character. Because in the cmd environment, some characters have special functions, such as >, > > for redirection, | for pipe, &, & &, | | for statement connection. They all have specific functions. If you need to output them as characters, echo >, echo |. The cmd interpreter treats them as characters with special functions rather than ordinary characters. At this point, these special characters need to be escaped: the escape character ^ is added before each special character, so to output these special characters, you need to use echo ^ >, echo ^ |, echo ^ | ^, echo ^ ^. And other formats to deal with
8. &
In general & means that two commands or statements are executed at the same time. For example, echo a&echo b will display both an and b characters on the screen. When several sentences have similar meaning or function and there is no difference in order, enabling & symbolic concatenation of these statements will increase the readability of the program.
9, & &, | |
This is a pair of command characters with diametrically opposite meanings. & & means that if the previous statement is executed successfully, the statement after it will be executed, while | | means that if the previous statement fails, the statement after it will be executed; in some cases, they can replace if. Else... Statement; for example:
@ echo off
Md test&&echo successfully created folder test | | echo failed to create folder test
Pause
The effect is equivalent to the following code:
@ echo off
Md test
If "% errorlevel%" = = "0" (echo successfully created folder test) else echo failed to create folder test
Pause
10. ()
Parenthesis pairs often appear in for and if statements, as well as in specific situations; requirements that belong to the statement format in for and if statements, such as:
① for% I in (statement 1) do (statement 2): in this statement, statement 1 must be surrounded by parentheses, while the parenthesis pairs of statement 2 can be discarded or retained as appropriate: if statement 2 is a single statement or multiple statements connected with &, & &, |, etc., parenthesis pairs can be discarded, and if statement 2 is a logical set of statements, parenthesis pairs must be retained, and Multiple statements must be written with broken lines For example:
@ echo off
For% I in (a b c) do echo% i&echo-
Pause
It can also be rewritten as:
@ echo off
For% I in (a b c) do (
Echo I
& echo-
)
Pause
② if condition (statement 1) else (statement 2): if there is no else part, the parenthesis pair in statement 1 is optional; if there is an else part, the parenthesis pair in statement 1 must be preserved, so whether the parenthesis pair in statement 2 is retained or not is similar to the previous point. For example:
@ echo off
Test.txt exists in the current directory of if exist test.txt echo.
Pause
@ echo off
If exist test.txt (there is test.txt in echo's current directory) else echo has no test.txt in its current directory
Pause
@ echo off
If exist test.txt (there is test.txt in echo's current directory) else (
There is no test.txt in the current directory of echo
Pause
Cls
Echo is about to create a test.txt file
Cd. > test.txt&&echo successfully created test.txt
)
Pause
The use of parenthesis pairs in specific ③ situations can not only make the code logical and readable, but may also reduce the amount of code. For example, when constructing multi-line text content with echo statements:
@ echo off
(
First line of echo
Second line of echo
Third line of echo
) > test.txt
Start test.txt
If you do not use parenthesis pairs, you need to use the following code:
@ echo off
First line of echo > test.txt
Second line of echo > > test.txt
Third line of echo > > test.txt
Start test.txt
11, +, -, *, /
In the set / a statement, these symbols mean addition, subtraction, multiplication and division. For example: set / a num=1+2-3, 4, 2, 5. It should be noted that these symbols follow the order of priority in mathematical operations: multiplication, division, addition and subtraction, parentheses, and directly ignore the decimal point, so the result of that formula is 1 instead of 0 or 0.6.
In addition, you may see the following words in the code: set / a num+=1, set / a num-=1, set / a num*=1 and set / a num/=1, which means accumulation, accumulation, multiplication, accumulation, step size is 1, and the complete words after expansion are: set / a num=num+1, set / a num=num-1, set / a num=num*1 and set / a num=num/1 (in the set / a statement, variable references can ignore the percent sign pair or exclamation mark pair. Set / a num=%num%+1 is equivalent to set / a num=num+1)
12 、 equ 、 neq 、 lss 、 leq 、 gtr 、 geq
These commands are the numeric comparison symbols commonly used in if sentences, which are taken from the key letters in English. The specific meanings are:
English interpretation of the meaning of command symbols
EQU equals equal
NEQ is not equal to not equal
LSS is less than less than
LEQ is less than or equal to less than or equal
GTR is greater than greater than
GEQ is greater than or equal to greater than or equal
This is the end of this article on "which symbols are commonly used in cmd batch processing". 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, please share it out for more people to see.
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: 0
*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.