In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use the set command in DOS batch processing. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
First, set custom variables with the set command
Displays, sets, or deletes the cmd.exe environment variable.
SET [variable= [string]]
Variable specifies the name of the environment variable.
String specifies a series of strings to assign to the variable.
To display the current environment variable, type SET with no parameters.
The SET command does not allow variable names to have an equal sign.
Note: the following usage clears the value of the variable variable, making it undefined.
SET variable=
There is no sign after the above equal sign. If it is written as SET variable= "", the value of the variable is not empty, but is equal to two quotation marks, namely "".
Example:
@ echo off set var= I am worth echo% var% pause
Look at set var= I am the value, this is how BAT sets variables directly in the batch!
Set is the command var is the variable name = sign to the right "I am the value" is the value of the variable
To refer to this change in the batch, expand the var variable name with two% (% sign), such as% var%
SET can also provide an interactive interface for the user to enter the value of the variable, and then we will do the corresponding operation according to this value. Now I will talk about the syntax of SET, just add a "/ P" parameter!
SET / P variable= [promptString]
Example:
@ echo offset / p var= Please enter the value of the variable: echo you entered% var% ~ _ ~ pause
Set / p is the command syntax var is the variable name = sign to the right of the "Please enter the value of the variable:", this is the prompt, not the value of the variable!
After running, we type 1 directly after the prompt, and a line will be displayed that you have entered 1 ~ _ ~
All right, let's review this first, and now let's talk about the other functions of SET.
Use set /? Looking at SET's help, we found that SET, in addition to what I mentioned above,
SET [variable= [string]]
SET / P variable= [promptString]
In addition to these two grammars, there are the following grammars:
SET / An expression
Environment variable substitution has been enhanced as follows:
% PATH:str1=str2%
% PATH:~10,5%
% PATH:~-10%
% PATH:~0,-2%
What is the use of this kind of grammar? Let's explain them one by one!
Second, use set command to do simple calculation.
Syntax: SET / An expression
The / A command line switch specifies that the string to the right of the equal sign is the evaluated numeric expression. The expression evaluator is simple and supports the following operations in descending order of priority:
()-grouping
! ~-- unary operator
* /%-arithmetic operator
+-arithmetic operator
>-binary logic shift
&-binary bitwise "and"
^-binary bitwise "different"
| |-binary bitwise "OR"
= * /% = + =-- arithmetic assignment
& = ^ = | =-binary operation assignment
,-expression delimiter
If SET / An is executed on a command line other than the command script, it displays the last value of the expression. Except for hexadecimal with 0x prefix, octal with 0 prefix, the numeric value is decimal. Therefore, 0x12 is the same as 18 and 022. Note that octal formulas can be easily confused: 08 and 09 are invalid numbers because 8 and 9 are not valid octal digits.
These are the contents of the system help, look a little dizzy, it doesn't matter I will briefly explain: set / A parameter is to enable SET to support mathematical symbols for addition and subtraction and other mathematical operations!
Note: the general operation is often a decimal operation, if the leftmost numeric string is 0, it will be considered octal, resulting in an error. For example, numbers such as 0812 cannot participate in decimal operations, and the conversion method is: 10812-10000
Example:
Set aa=0812set / an aa=1%aa%-10000echo aa%
The result is: 812
Example:
@ echo offset / p input= Please enter the calculation expression: set / a var=%input%echo calculation result:% input%=%var%pause
The above example is designed by tornadoes and is easy to use. please take a look at the following operations:
Note: DOS calculation can only operate on integers, accurate to integers.
Please enter the calculation expression: 1 "9" 20 "30-10"
Calculation result: 1: 9: 20: 30-10: 50
Please press any key to continue. . .
Please enter the calculation expression: 10 stroke 3 # division can only be accurate to an integer
Calculation result: 10 inch 3 inch 3
Please press any key to continue. . .
Please enter the calculation expression:-100mm 62 # negative number
Calculation result:-100mm 62m / m 38
Please press any key to continue. . .
Please enter the calculation expression: 100% 3 # for the remainder
Calculation result: 100% 331
Please press any key to continue. . .
Note: the above remainder operator% must be written as%% in the batch program.
Please enter the calculation expression: (25075) * 2 / (1505) # parentheses
Calculation result: (25: 75) * 2 / (15: 5) = 10
Please press any key to continue. . .
Please enter the calculation expression: 1234567890 "9876543210 # range
Invalid number. Digital accuracy is limited to 32 bits.
Calculation result: 123456789098 76543210 =
Please press any key to continue. . .
Note: the above calculation process shows that the DOS calculation can only be accurate to 32 bits, which refers to the binary 32 bits, where the highest bit is the sign bit (0 is positive, 1 is negative) and the low bit 31 is the value. Change 31 1s to decimal 2147483647, so the range of valid values calculated by DOS is-2147483648 to 2147483647. An error occurs when the value is beyond this range. Please see the following calculation process:
Please enter the calculation expression: 2147483647-1 # maximum minus 1, the value is valid
Calculation result: 2147483647-1147483646
Please press any key to continue. . .
Please enter the calculation expression: 2147483647mm 1 # maximum plus 1, error, the result is minimum
Calculation result: 21474836477147483648
Please press any key to continue. . .
Please enter the calculation expression:-2147483648-1 # minimum minus 1, error, the result is the maximum
Calculation result:-2147483648-1147483647
Please press any key to continue. . .
After running set / an axiom1meme 1memoir 2meme1recorder 3q1, we will display a 4, but if we look at the result with echo% a% b% c%, we will find that other mathematical operations are also effective! this is the function of the expression separator "funny"!
Sometimes we need to add and subtract directly from the original variable to use this syntax set / a var+=1 corresponding to the original syntax is set / a var =% var% + 1
It's all the same result. We're doing a mathematical operation on the value of the original variable, but it's a little easier to write one more:
Set / a var*=2
Everything else is used in this way, as long as there is this grammar in the help!
In addition, some use logic or remainder operators, and these symbols will report errors according to the above method of use.
For example, if we type set / a var=1 & 1 "and operation" in CMD, it will not display as 1, but will report an error. Why? For such "logical or remainder operators", we need to enclose them in double quotation marks, or we can use the escape character ^. See the example.
Set / a var= 1 "&" 1 so that the result is displayed, other logic or remainder operator usage
Set / a var= 1 "+" 1 different operation
Set / a var= 1 "%" 1 modular operation
Set / a var= 3 "" 2 right shift operation, the binary of 4 is 100, the right shift 2 bits is 1, the result is 1
Tornado added: all bitwise calculations need to be converted to binary.
Thinking question: find the n power of 2
Reference answer:
@ echo offset / p n = Please enter the power of 2: set / a num= 1 ^
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.