A detailed explanation of the cmd batch escape character%

Source: Internet
Author: User

A detailed explanation of the cmd batch escape character%

Using for at the command line does not require double%, which stems from the way the command interpreter handles the command line and batch processing.

1,% is an escape character, usually translated as an escape character, but also has a more image of the characters, escape characters and so on. That is, the% will not only escape the specific string associated with it and replace it with a specific string, but will also be "off" itself. and similar to the escape character "\" In C, the double will escape and take off as a single, and four off to double.  

2, for itself is a special command, similar to a specialized command interpreter, because its function implementation needs to execute multiple statements, Therefore it must also have the ability to parse the command line (specifically, the command line after do). While Command/cmd implements a for, it naturally borrows its own original command-line parsing module, so for has a two-level escape feature, the for-do statement is parsed and interpreted at two levels, and the first level is read into and interpreted for the for command row in Command/cmd. The second level usually interprets the same command line several times when the for reads in and interprets the Do command.  

Then, we can notice that when you use command-line argument variables and environment variables in do, you do not need a double, because these variables are escaped through the first level. is replaced with a specific invariant string constant that participates in all the execution of the For loop, while the substitution variable requires constant dynamic changes in the execution (in the sub-command line of Do), which naturally still needs to be implemented by means of the escape character, so the use of double is a necessary choice.  

In addition, it is important to note that there is no need for double% to use for at the command line, which stems from the way the command interpreter handles the command line and batch processing differently. In earlier versions of DOS,% was not considered an escape character in the command line, so it was not escaped and was removed, so the environment variable could not be referenced directly at the command line at that time. When using for, only one% is needed for the for to escape and the release is sufficient. In a later version of the command interpreter, support for command-line escaping was added (primarily support for environment variables), but the tradition of command line for single use was still preserved.  

While variable delay substitution in CMD is a special case, it does not violate the above escaping principle, except that the environment variable in for is no longer a constant.

Case:

I have a bat file on my own, content:

@echo off

If "%1" = = "(goto input_data) ELSE (goto Input_var)

: Input_data

set/p var=input DATE (FMT:YYYYMMDDHHMM):

Goto Start

: Input_var

set/a var=%1

Goto Start

: Start

CD%cd%

Md.. \.. \to-save\tkcard\%var%

Mysql-p3302-uroot-Pdst72j$mq) c%%8 -h61.160.245.119-n-s-e "SET SESSION Group_concat_max_len = 99999 999; SELECT CONCAT (' mysqldump -h61.160.245.119-p3302-uroot

-Pdst72j$mq) c%%%%8 --opt-n tkcard_cn_cn_db210060000 ', Group_concat (T.table_name SEPARATOR '), ' > ') from INFORMATION_SCHEMA. TABLES t WHERE T.table_schema =

' tkcard_cn_cn_db210060000 ' and (T.table_name like ' dict_%% ') and T.table_name not in (' Dict_keyvalue ', ' dict_server ', ' Dict_operator_config '); "> Tkcard_dict.bat

For/f "delims=] Tokens=1"%%a in (Tkcard_dict.bat) do echo%%a. \.. \to-save\tkcard\%var%\tkcard_dict.sql >tkcard_dict.bat

Call Tkcard_dict.bat

The top function is to guide all the underlying data in the remote specified database. The file writes a MySQL connection database script, and in this script through the "-e" parameter to export all the underlying table data script code (that is, the mysqldump command), the results do not achieve their intended effect, analysis of the reason, the operation of the intranet database can be, Change the IP address to the remote database address is not, analysis to analyze to the last is the database connection password problem, that is, password network password no special characters%, and the remote this has%, the result is this% no special escape out of the problem, the top of the footsteps involved 2 of the same password, a place is To connect to the database password, but also a database in the export of all the underlying data with the mysqldump command when the same password, the bat file a total of 2 passwords, and all the same, the password is also involved in the%, the result I just did not have a special escape to the 2 of the problem. The top is already correctly escaped, the original password is:dst72j$mq) c%8,The first place to be correctly escaped is to add a% before the% and the second is the need to add 3 before the%. Due to the article at the beginning of the% escape rule, 2% escaped once for%, 4% escaped after 2%, then here the first place to properly connect the database to ensure that the password is the original password is a% of its own, it must be added a%, so that the first password there is 2%, when the Windows command line When the interpreter executes the connection database, the 2% is escaped, which is exactly the same as the original password, and the second password on the top is also guaranteed to be the same as the original password. As for adding a few, let's first take a look at the logic of the second place above. The top one is the connection database, and the suffix mysql parameter command-e is the execution of the following SQL script, that is, to connect the database and then execute the script, the structure of mysql-p[capital P for Port]-u[connection Database account]-p[lowercase P for the connection database password]-h[database IP ground Address]-n[do not display column information]-s[a line of output, the middle tab separated]-e[execute MySQL SQL statement, followed by double quotes, double quotation marks in the SQL script code] "[double quotation marks are the front-e followed by SQL script-this ellipsis explained below]" >tkcard_dict.bat[ The results of the above SQL execution are directed to the Tkcard_dict.bat file, because there is no path before Tkcard_dict.bat, that is the current directory, if there is no Tkcard_dict.bat file created, there is overwrite] for ... [For loop starts execution of Tkcard_dict.bat file].

In particular, the script in the double quotes above-E is interpreted: SET SESSION group_concat_max_len = 99999999; SELECT CONCAT (' Mysqldump-h61.160.245.119-p3302-uroot

-Pdst72j$mq) c%%%%8--opt-n tkcard_cn_cn_db210060000 ', Group_concat (T.table_name SEPARATOR "), ' > ') from In Formation_schema. TABLES t WHERE t.table_schema =

' tkcard_cn_cn_db210060000 ' and (T.table_name like ' dict_%% ') and T.table_name not in (' Dict_keyvalue ', ' dict_server ', ' Dict_operator_config '); "

Means to set the Grop_concat function to the length of this query session, and then select  concat ( Span style= "color: #ff0000; Background-color: #ffff00; " >mysqldump code-The code is spelled as a string constant and this does not execute  ,   group_concat (from INFORMATION_SCHEMA. The TABLES table is identified by the Dict base table name that is fully found in the Where condition to use a space interval and to spell a string)-the result of the entire group_concat as an argument to the concat function of the front      ,   >   )      

You can see that the step in the footsteps of the-e double quotation marks is mainly from INFORMATION_SCHEMA. TABLES (the TABLES table in this database is the name of all the other databases in this MySQL server except Information_schema, as well as all the table names in all the databases, specifically the TABLES table Table_ The schema field is the database name, the tables table in the TABLE_NAME field is the name of the table in each database) table, according to the database name and data table name given in the Where Condition 2 criteria to query the table name of the underlying data table, The underlying data table name is then identified by using the Group_concat function with a space interval and to spell a string, and then the string with the mysqldump command and a ' [there is a space] > ' A total of three parameters through the CONCAT function into a final string, and then mutual Redirected to the Tkcard_dict.bat file via "> Tkcard_dict.bat", the contents of the Tkcard_dict.bat file are ultimately: Mysqldump-h61.160.245.119-p3302-u Root

-Pdst72j$mq) c%%%%8--opt-n tkcard_cn_cn_db210060000 Basic table 1 Basic Table 2 basic table 3 ... >

As for the code behind the for is not very clear, the estimate is to execute the contents of the newly generated Tkcard_dict.bat file above, and its content is just a mysqldump script, so the mysqldump script after the execution of the results are imported into The Tkcard_dict.sql file in the custom path. As for the last ">tkcard_dict.bat" in the final top bat code, I don't know what to do with it.

A detailed explanation of the cmd batch escape character%

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.