Automation for UNIX / Linux platform tasks
Reprinted from: Shuimu Tsinghua BBS
This chapter points:
This chapter describes tools used to replace the shell script, such as TCL and Perl.
This chapter specifically includes the following.
TCL / EXPECT
AWK language basic knowledge
Basic knowledge of Perl language
11.1 TCL and EXPECT
TCL is a language similar to a shell script that you can use to complete a number of operations. However, I introduced it.
The main reason is that Expect is developed from it. If you want to write a script that can automatically process the input and output
(If you ask questions to users and verify passwords), you don't want to face C or Perl, then EXPECT is your unique choice.
11.1.1 TCL Language
To use TCL, you must install this program first:
% Rpm -q tcl
TCL-8.0.5-30
The TCL language can be performed in a way to interactively or scripts, as long as you enter the interactive TCL environment.
$ TCLSH
%
The "%" symbol that appears is the TCL prompt, and then you can use the Tcl command.
If you want to use a script's Tcl, first write your script into a text file, such as Test.TCL,
Afterwards
$ Tclsh Test.tcl
In the TCL script, each row or a command line, or a comment. Note Walk must start with # symbol
And the command line is best to end with a semicolon, although it is not necessarily doing this, but this can be exempt from a lot of trouble.
variable
In Tcl, there are two basic types of variables, namely, and arrays. The scalar is a general number or string
Quantity, you can define simultaneous assignments in the SET statement:
% Set i 1
1
Strings should be enclosed in quotes:
% Set Str "Test"
'Test'
To output a scalar, use the PUT statement:
% PUTS $ STR
Test
$ Is used to explain that Str is a variable. The PUTS function outputs the content of the display variable in the standard output.
An array can also be defined with a set statement. In fact, the establishment of an array is only a single set of elements in the TCL. E.g
,
% Set Arr (1) 0
0
% SET ARR (2) 1
1
This creates an array Arr of two elements. In Tcl, there is no thing equivalent to array boundary.
,E.g
% Set Arr (100) to
TO
At this time there is actually only Arr (1), Arr (2) and Arr (100) in the array, which is different from the C language. Use Arr
The AY size command can return the size of the array:
% Array Size Arr
3
The method of accessing arrays and access standards is the same, for example:
% Puts $ ARR (100)
TO
You can create multi-dimensional arrays in the same way.
To use all elements in an array, you need to use a special convenience. The first thing to start StartSEARSH:
% Array StartSearch Arr
S-1-Arr
Here is a search id, you can pass it to a variable, because it will be used in the future.
Step search:
% Set my_id [Array StartSearch ARR]
S-1-Arr
Now my_id content is S-1-Arr, then you can search for the contents of Arr:
% Array NextElement Arr $ my_id
WHI
What is the Array nextElement here? Maybe it is a bit unexpected, it is the subscript of the Arr array
, Then execute an array nextElement command to find another subscript:% array nextElement Arr $ my_id
4
In this way, you can find all the subscripts of the Arr array, and you can use $ ARR (4) after you know the subscript.
The way the class has access the contents of Arr. When the traversal is complete, the Array nextElement command will simply return:
% Array NextElement Arr $ my_id
%
At this time, you can stop traversing the traversal process. If you want to confirm that the traverses is completed, you can use Array Anymore.
make:
% Array Anymore Arr $ my_id
0
Returns 0 Description Traverse has been completed.
String processing
A general string processing process can be performed in the TCL, which can use the string command and the append command, the append command
Add a string to another string:
% Set Str1 "TEST"
Test
% Set str2 "cook it"
Cook IT
% Append str1 $ str2 "and other"
Test cook it and other
The string command can perform the comparison, deletion, and query of the string, which format is string [parameter] String1
[String2]
The parameter can be one of the following commands:
Compare the string according to the order of dictionary, returns -1,0 or 1 according to the relative relationship.
First returns the position of String1 in String2, if it fails, returns -1.
Last Returns the last time String1 in String2, if it fails, return -1
Trim removes the character in string2 from string1 and ending characters in string2
TrimLEFT removes the characters in String2 from String1.
Trimright The characters in String2 from String1 from String1
The following severings do not require String2 variables in String:
Length returns the length of TRING1
TOLOWER Returns a string that smings String1 small
TouPper returns a string of all the String1
Calculate
The operation method of the Tcl is a relatively awkward. It uses the expr command as a calculation symbol, which uses the usage similar to the C language = and / =
,E.g,
% Set J [EXPR $ I / 5]
1
Note that TCL will automatically select an integer or floating point calculation:
% Set L [EXPR $ I / 4.0]
1.25
% Set L [EXPR $ I / 4]
1
In TCL, - * / and% can be used as basic operators, and some mathematical functions are generally generally included, such as A
BS, SIN, COS, Exp, and Power, etc.
In addition, there is a command INCR for start operators, which is used to add a variable:
% Set i 1
1
% Incr i
2
Process control
TCL supports branches and loops. The branch statement can be implemented using IF and Switch. The IF statement is similar to the C language, such as
IF {$ x <0} {
SET Y 10;
}
Note that judgment clauses also need to use curly brackets.
Like the C language, the IF statement of the TCL can also use ELSE and Elseif.
The usage of the Switch statement is a bit like this:
Switch $ x {
0 {set y 10;}
10 {set y 100;}
20 {set y 400;}
}
Unlike the Switch statement of C, each time the subsee of the branch value is executed.
The loop command is mainly composed of for, foreach, and while, and each of Break and Continue can be used.
Clause.
The format of the for statement is a bit like this:
For {set i 0} {$ I <10} {incr i} {PUTS $ I}
The integer from 1 to 9 will be output.
If you use the While loop, this sentence can be written
While {$ I <10} {
Puts $ i;
INCR I;
}
Foreach is an executive command for each element in a collection, the general command format is
Foreach [Variable] {Collection} {
Statement;
}
E.g
% Foreach J {1 3 5} {
PUT $ J;
}
1
3
5
function
As in the general programming language, the function can also be defined in the Tcl, which is implemented by the proc command.
of:
Proc my_proc {i} {
Puts $ i;
}
This defines a function of a name called Proc, which is just displaying the content of the input change in the terminal.
To use this function, simply enter its name:
% MY_PROC {5}
5
If the number of variable elements is 0, as long as the empty variable group list, such as the proc my_proc {} {statement;}
Although TCL can also handle more complex processes, we will no longer introduce, such as file read and write and TK graphics
Language, because we deal with the main goal of TCL is to understand EXPECT, for more complex programming, we recommend
You use Perl.
11.1.2 Expect
Expect is a tool based on TCL, which is used to automate some tasks that need to interact.
We first start from a simple example, as mentioned in this section, we want to set an automatic
File download program.
Let's take a look at this example script:
#! / Usr / bin / expect
Spawn ftp 202.199.248.11
EXPECT "Name"
Send "ftp / r"
Expect "Password:"
Send "Nothing / R"
EXPECT "APPLY"
Send "CD / PUB / UNIX / Linux / Remotex / R"
Expect "Successful."
Send "bin / r"
EXPECT "set to i"
Send "get exceed5.zip/r"
Expect "Complete."
Send "QUIT / R"
what does this mean? Oh, it is an automatic download program. The first line of explanation This program should be called / usr / b
IN / EXPECT is executed, then the expect command.
Look at the Expect's Manual page (Man Expect) can get a long Expect note, but unfortunately
The syntax of Expect is still insufficient. In general, Expect is mainly used in need to automate human-computer interaction.
In the process, such as an FSCK program, this program will continue to ask "Yes / no", like this, you can use Expect
To be done.
The spawn statement is used in the Expect script to start a new process, in our program, SPAWN FTP 202
. 199.248.11 is to execute the FTP program, then, that is, the instructions of Expect and Send are correct.
Each pair of Expect and Send instructions represent an information / response. If this is not understood, then you can see
See the specific implementation process of FTP:
FTP 202.199.248.11
CONNECTED TO 202.199.248.11.
220 mail.asnc.edu.cn ftp server (beroftpd 1.3.3 (3) Sun feb 20 15:52:49 CST
2000.
Name (202.199.248.11:wanghy):
Obviously, once the connection is successful, the server will return a Name (202.199.248.11:wanghy): string
Require customers to give username. The EXPECT statement simply queries the string you given in return information, once successful
Just execute the following command, now, Expect "Name" has successfully found the Name string, next to
Execute the send command.
The send command is simpler than the expect command, it simply submits your set string to the standard input, now set
For the Send "FTP / R", give an input FTP carriage return after the login information is given, that is, the standard login
Process.
The following rows are exactly the same as these lines, just mechanically waiting for the server to respond, and submit your own input.
To use this expect script, you only need to set it to the executable properties, then execute it, Expect
Execute the services you need.
Since Expect is an extension of TCL, you can set variables and programs like TCL scripts in an Expect file.
Process.
Now let's take a look at how we can improve our Expect scripts. FTP commands may fail, such as far
End machines may not provide services or problems with local machines when the FTP command is started. In order to deal with this
The problem of class, we can use the expect's Timeout option to set the timeout, the expect script is automatically exited:
#! / Usr / bin / expect
Spawn ftp 202.199.248.11
EXPECT {
Timeout exit
Connect
}
..................
Note that the curd brackets used here are. Its meaning is to use a set of parallel expressions. Mainly used in parallel expressions
The reason is this: If you use the following instructions:
Expect Timeout
exit
Then because the Expect script is executed in order, then it will block when the program executes this expect.
So the program will wait until TIMEOUT and then exit. The side-column expression is equivalent to the behavior of Switch, as long as the column
A few items have a satisfaction, the expect command is met, so the program can be implemented normally. Above
The script represents that if the ftp is timeout, then exit, otherwise, once the connection should be found
A, explaining the server is normal, then you can continue to run.
We can see what help can be used with TCLs to provide our Expect scripts. We can set up the expect feet
This continuous connection to the remote server service until the connection starts normally, for this, we can connect the establishment
The command is placed in a loop and automatically selects the command according to the response to the re-enter command or continue:
Spawn FTP
While {1} {
EXPECT "ftp>"
Send "O 202.199.248.11/R"
EXPECT {
Connected "Break
"refuse" {SLEEP 10};
}
}
Here, we use the While and Break commands in the TCL language. Readers who are familiar with C should easily see its behavior: constantly waiting for the FTP> prompt, send a command to connect the remote server under the prompt, if clothes
The instructor response is REFUSED, wait 10 seconds, then start the next loop; if it is Conne
CTED, then jump out the loop to perform the following command. Sleep is a standard command for Expect, indicating that a number of suspensions
Second.
EXPECT also supports many more complex process control methods such as Fork, Disconnect, etc. You can
Detailed information is obtained in the page. In addition, various TCL operators and process control commands, including TCL functions,
use.
Some readers may ask if the expect executes if the console input cannot be used, the answer is negative.
. When the expect command is running, if a waiting message is not got, the program will block the corresponding Expect
Statement, at this time, what you entered on your keyboard can still be transferred to the program normally, in fact for those
Expect processing information, in principle, the content you entered is still valid, just the reflection of Expect is too fast, always grab
Your front "input" is what. After knowing this, you may write an Expect script to let Expect
Automatically handle those nauseaful YES / NO options from FSCKI (we introduced, these Yes / no is actually exactly
Under normal circumstances, you can't do anything other than choosing Yes).
Under the default, Expect extends all the response information from the application in the standard output (your terminal), you can
Redirect this with the following two commands:
LOG_FILE [file name]
This command allows Expect to record output information in your file. It must be noted that this option does not affect control.
Table output information, but if you set the expect script through crred in the middle of the night, you can do it.
You want this command to record a variety of information. E.g:
Log_file expert.log
LOG_USER 0/1
This option sets whether the output information is displayed, and it is set to 1, if it is 0, Expect will not produce any
What output information, or simply filter out the console output. I must remember if you use log_user 0 to close it.
The console output, then you turn off the output of the record file.
This is very troublesome, if you really want to record the output of the expect, don't want it to make garments on the console
If you are cloak, you can simply redirect the output of Expect to / dev / null:
./test.exp> / dev / null
You can use a pair of forks and disconnect commands like this. The disconnect command of the expect will make
The corresponding process is executed in the background, the input and output are redirected to / dev / null:
IF [fork]! = 0 exit
Disconnect
The fork command generates a child process, and it generates a return value, if it returns 0, this is a child
Process, if not 0, then the parent process. Therefore, after executing the fork command, the parent process dies and the sub-process is
The disconnect command is on the background execution. Note that the disconnect command can only be used to the child process.
11.2 Treatment of awk and files
The Unix is filled with various record files and similar things. The processing of text files is that system administrators are heavy every day.
To work, for example, find important content from the system record, or statistics on the output of some programs. We will introduce a commonly used handler, namely Gawk.
11.2.1 GREP and regular expressions
Let's start with the GREP command. This order should be very familiar, it is used to find one in the file.
String. However, in fact, GREP's processing functions are much stronger and complicated.
The syntax of the grep command is
GREP [Mode] [File Name]
If the file name is not given, the standard input is used by default. GREP reads a row each time, and and gives the mode
Match, if success, this line will be displayed, for example: (bold is what we entered)
$ GREP TEST
Close
Test My Hand
Test My Hand
The "mode" of GREP is also referred to as regular expressions, which can be constructed of various substantially regular expression elements. Regular table
The Dare element mainly includes several of the following:
String matches any string, for example, GREP TEST is represented in standard input 1
[...] Closed Connect Match a character, such as: [Abcde] can match A, B, C, D, E
[^ ...] seeking a set of characters, such as [^ ABC] match
Match any character
/ s blank character
/ S non-blank character
/ d number
/ D non-number
/ w letters or numbers
/ W non-letter and numbers
* Match any character
The above form is the basic regular expression used in GREP, in addition, EGREP can also be used, EGREP is GREP
An extension version that supports the following regular string:
^ Match a line of start
$ Matching the end of a line
() Determine the order in which the regular expression is evaluated, and the brackets in normal calculations are similar.
(... | ... | ...) or one of the options, for example: (ABC | DEV | GHI) Match ABC, DEV, GH
i, and (WW | GG) Do I can match WWDO or GGDO.
One or more mode
Such as: ABA matches ABA, Abaa ... does not match AB
Usually, we have two ways to use grep and egrep, one is to use pipes, such as PS we should be familiar.
AX | GREP Sendmail, the other is to search directly in the file to search the corresponding string.
GREP / EGREP can also use the switch in the command line, and the commonly used switches include:
-b plus the block before row
-C statistics match line
-n plus line number before row
-w Interpret the pattern as a string, all regular expressions have failed
-x Exactly Match
-r Query file When the sub-directory
For example, we want to query all from / var / log / httpd / access_log from local (192)
.168.0.1) Request records, can be executed:
GREP -V "^ 192.168.0.1" / var / log / httpd / access_log
^ It is used to make GREP only match the lead.
You can use wildcards to represent multiple files when GREP queries, for example, grep start * -r will be currently
String strings in all files of all subdirectories.
11.2.2 How to use Gawk
Gawk is an implementation of awk, AWK is a scripting language for handling text files such as reports. However, we
Introducing the main goal of this product is to use it to handle the accounts files for various programs. For complex scripts, it is still suitable with Perl.
The main function of Gawk is to search for the specified mode for each row of the file. , Whenever a matching mode
Gawk will perform the action you set. According to this way, Gawk handles each of the input files in this way.
Direct until the end of the input file. If there is no corresponding action for a mode, Gawk will directly
It is shown.
In order to use Gawk, you usually have to write an AWK script, unless the mode / action is very simple, you can be in one line.
Finished. We use an example to explain the basic usage of Gawk, first generate a list of directory lists:
LS -L / ETC> List
The content of List is a bit like this:
Total 2164
DRWXR-XR-x 3 root root 4096 Feb 15 22:55 Corba
-rw-r - r - 1 root root 2045 Sep 24 1999 DIR_COLORS
-rw-r - r - 1 root root 17 mar 25 19:59 hostname
..........
Now we choose a simplest example, simply find all the properties is a directory file for DrwxR-XR-X:
Gawk '/ DRWXR-XR-X / {Print $ 0}' List
The output will be output.
This example seems to have no practical use, because you can do the same action with GREP, then we can
Take a look at this feature:
$ GAWK '$ 1 == "- RWXR-XR-X" {SUM = SUM $ 5} End {print sum}' list
15041
what does this mean? For all attributes, 755 files, let Gawk seeks the number of the fifth column. Fifth column I
We can see that the length of the file, so this command will display a total length of the file with 755.
$ N is a very important concept in Gawk, which is used to represent the column of the text string. By default, Gawk will enter
String (each line read from the file) divided into several fields according to the split space, each field as one
Variables, such as one line
My name is 3th Test
So, after AWK read this line, there was a $ 1 to $ 5 variable, of which $ 1 = "MY", $ 2 = "IS", .........
, Last $ 5 = "Test". There is also a special variable $ 0, which represents the entire input line, that is, this string
"My Name is Test". There is also a special variable NF, which represents the number of fields of the current line, in now
In the case of, NF should be equal to 5.
In some special cases, you may need to change the definition of the division, which can be done by assigning the FS.
For example, fs = "," "Defined the splitter as the number number rather than the default space.
In general, Gawk can get the mode / action from the command file, the format of the command file is simple, that is
You should write the mode / action on the command line to write in the file, each pair, the mode can be two
The species, one is the pattern match, that is, our regular expression in the previous explanation, if the regular expression is used, then
It is necessary to use two / put them together, such as / [a-z] / represents the regular expression [A-Z]. Another mode is comparative instructions, and the comparison instructions can be composed of comparison operators and logical operators.
More operators are:
== equal to <= no greater than ~ follow the regular expression
> Bigger than! = 不 等
Logic operator
&& and || or! Non () brackets
After setting the mode, you can set the corresponding action. In Gawk, the action must be enclosed with a curly bracket. GA
There is not much action on WK, after all, it is a report analysis language. Under normal circumstances, as long as it is familiar with PRINT and P
The RINTF command is enough, and the format of the print command is very simple:
Print Item1, item2, ............
When output, each item outputs a column, separated by spaces. A print is not followed by any variables
Gawk displays the current input line ($ 0). If you want to output a string, use quotation marks to enclose it, especially
If you want to output an empty line, use Print "". Here is an example, it outputs the header of the list file:
Gawk '{Print $ 1, $ 2}' List
Since the text files entered have multiple lines, the mode / action you designed in the command bar will be executed once.
. Is:
Total 2164
DRWXR-XR-X 3
-rw-r - r - 1
-rw-r - r - 1
-RW-R - R-1
.......................
If you want to accurately control the output, you can also use the printf command, this command's format is:
Printf Format, item1, item2, ...
The format parameter is the format control in the C language, such as% C,% D,% F, etc. Control in% and format
Modifier can be added between letters, and Modifier is used to further control the format of the output. Possible modifie
R As shown below:
'-' Before using Width, it indicates to lean to the left. If '-' does not appear, it will be specified.
Based on the width. E.g:
Printf "% -4s", "foo" will print 'foo'.
'width' This number indicator is printed with the width of the corresponding field. E.g:
Printf "% 4s", "foo" will print 'foo'.
The value of Width is a minimum width rather than the maximum width. If an item value needs to be required
Big than Width, it is not affected by Width. For example, Printf "% 4s", "Foobar" will print 'FOOBAR'.
'.prec' This figure specifies the accuracy of printing. It specifies the number of bits on the right side of the decimal point. If it is printed
String, it specifies how many characters will be printed by this string.
As a scripting language, Gawk allows the use of variables, defining variables are very simple, which is directly used by the equal sign.
value. In order to assign variables in the beginning of the Gawk program, Gawk provides a Begin statement, which will be in the
Execute the row before being read, and only once, usually use it to perform the initialization command, for example
Begin {SUM = 0; count = 0; AVERAGE = 0.0;} For variables, mathematical expressions can be used, and operators include common addition and subtraction and subtraction, and ^ (multiplication)
Party),% (more) and famous , However, pay attention to Gawk always uses floating point divisions when doing elimination.
Drive the coda%.
function
In addition, Gawk contains the following functions:
Mathematical function
Atan2 (x, y) Y / x
COS (X) cosine function
sin (x) sinusoidal function
INT (x)
Log (x) Take a natural logarithm
Exp (X) index function
Rand (x) generates a random number between 0 and 1
SRAND () initializes the random number generator
SYSTIME () Returns the number of seconds from January 1, 1970 to the current time
SQRT (X) Take X's square root
String function
Index (String1, String2)
It will be in String1, find the first place in String2, the return value is the string string2
The location of the string string1 now. If you can't find it, the return value is 0.
E.g:
Print Index ("Peanut", "AN")
3 will be printed.
Length (String)
String string length
E.g:
Length ("abcde")
It is 5.
Match (string, regexp)
The Match function will be in the string string, looking for the longest and left son-on
string. The return value is regexp at the beginning of String, ie index value. This function sets internal variables
RStart is equal to INDEX, internal variable RLength is equal to the number of conformable substrings. If it does not match, it will be set
RSTART is 0, and RLEngth is -1.
Sprintf (Format, Expression1, ...)
The Sprintf with the C language is similar.
E.g:
Sprintf ("pi =% .2f (approx.) ', 22/7)
The back of the string is "pi = 3.14 (approx.)
SUB (Regexp, Replacement, Target)
Inside the string Target, look for the longest and leftmost place in accordance with Regexp, and string
Replacement replaces the leftmost regexp.
E.g:
Str = "Water, Water, Everywhere"
SUB (/ at /, "ith", STR)
Result string STR will become
"WITHER, WATER, EVERYWHERE"
GSUB (Regexp, Replacement, Target)
GSUB is similar to the previous SUB. Inside the string Target, look for all parts that meet regexp
Instead of all Regexps in string replacement.
E.g:
Str = "Water, Water, Everywhere"
GSUB (/ AT /, "ITH", STR)
Result string STR will become
'Wither, Wither, Everywhere
Substr (String, Start, Length)
Retrieve the string string of String, the length of this sub-string is Length characters, from the START
Start with a position.
E.g:
Substr ("Washington", 5, 3) The return value is "ing"
If the length does not appear, the transmitted subster string starts from the position of the START to the end.
E.g:
Substr ("Washington", 5)
The return value is "INGTON"
TOLOWER (String)
Change the uppercase letters of the string string to lowercase letters.
E.g:
TOLOWER ("Mixed Case 123")
The return value is "Mixed Case 123"
TouPper (String)
Change the lowercase letters of the string string to uppercase letters.
E.g:
TouPper ("Mixed Case 123")
The return value is "Mixed Case 123"
Other functions
SYSTEM (COMMAN)
This card allows the user to perform an instruction of the job system, will return to Gawk after execution
Program.
E.g:
Begin {system ("ls")}
Control flow
The control stream can be used in the Gawk command script, mainly if, for, while, etc., usage and C language is quite
similar:
IF (condition) THEN-BODY [ELSE ELSE-BODY]
If the condition is true (TRUE), then then-body is executed, otherwise else-body is executed.
To give an example, as follows:
IF (x% 2 == 0)
Print "x is even"
Else
Print "x is odd"
WHILE (CONDition)
Body
While statement tests the Condition expression. If the condition is true for true, the statement is executed. once
After execution, the Condition will be tested, if the condition is true, the body will be executed again. This one
The process will have been repeated until Condition is no longer true. If the first test of Condition is pseudo (FALS
e), then Body never executed.
The following example prints the top three fields of each input row.
Gawk '{i = 1
While (i <= 3) {
Print $ I
i
}
} '
DO
Body
WHILE (CONDition)
This Do Loop performs Body once, then as long as the cons correspond is true, the body will be repeated. even if
At the beginning, Condition is a pseudo, and the body will be executed once.
The following example will print each input recording ten times.
Gawk '{i = 1
Do {
Print $ 0
i
} While (i <= 10)
} '
For (Initialization; Increment)
Body
This narrative will perform Initialization, then as long as the condition is true, it
Repeat Body and do Increment.
The following example prints the top three fields of each input record.
Gawk '{for (i = 1; i <= 3; i )
Print $ I
} '
Break will jump out the innermost innermost in which it contains it, while.
The following example will find the minimum divisor of any integer, which will also determine if the rigid number.
Gawk '# Find Smallst Divisor of Num
{NUM = $ 1
For (DIV = 2; Div * Div <= NUM; DIV ) IF (NUM% DIV == 0)
Break
IF (NUM% DIV == 0)
Printf "Smallest Divisor Of% D IS% D / N", Num, DIV
Else
Printf "% d is prime / n", num} '
Continue is used inside for, while, do-while loop, which will skip the remainder of the circulation
Implement the next cycle immediately.
The following example is printed with all the numbers of 0 to 20, but 5 will not be printed.
Gawk 'begin {
FOR (x = 0; x <= 20; x ) {
IF (x == 5)
Continue
Printf ("% d", x)
}
PRINT ""
} '
Next statement forcing Gawk immediately stops processing the current line and continue the next input line.
The EXIT statement will make the Gawk program to stop executing. However, if End appears, it will go
Row END ACTIONS.
Custom function
You can define your own function, the format is
Function name (parameter-list) {
Body-of-function
}
Name is the defined function name. Parameter-List is a list of variables for functions. Both variables
open.
The function can be defined anywhere in the program, but the habits always define the beginning of the program.
Below, this example, the square of the first field of each record is added to the second field value
stand up.
{Print "SUM =", SquareSum ($ 1, $ 2)}
Function SquareSum (X, Y) {
SUM = x * x y * y
Return Sum
}
If you are familiar with any programming language, then master the awk is a very easy thing, if you don't like it, then you
You can refer to the Perl described below.
11.3 Perl
Perl has developed from awk, which is invented by Larry Wall in 1986. It is a powerful programming language
Words, and you can be used on many platforms. In fact, you can make Perl as a standard programming language (
Instead of scripting languages), I like it very much, and I recommend all UNIX administrators who don't want to learn C.
The basic programming technology of Perl should be grasped. Currently, the common version is Perl 5, almost all Linux distributions
Will contain it, when default, Linux's Perl 5 is installed under / usr / bin, the command is / usr / bin / perl.
11.3.1 Basic Law
Perl's syntax is between C and Basic, and a Perl program consists of several lines, and is interpreted by Perl.
Program interpretation execution. Each complete line should end with a semicolon.
The basic syntax of Perl is this:
1 variable and operator
In Perl, all variables do not need to be declared in advance. Once the value is assigned to a variable, it will automatically generate this change.
the amount. Perl's variables have three common variables, arrays and associated arrays. Ordinary variables are numerical and string,
Ming a normal variable, plus $ with a variable name, for example
$ String1 = "AAA";
$ TEST = 5;
$ u = 1.33;
Similarly, the access variable content also needs to use the $ symbol.
Array with @ character markers, such as
@ Name1 = ("Tom", "marry", "john");
$ b = $ name [0]; $ b is now equal to "Tom"
$ b = @ name [0]; following the same sentence is the same
$ Name [0, 2] = ["Help", "so"]; now @name is equal to ["Help", "Marry", "SO"]
@name [0, 2] == @ Name [2,0]; exchange 0,2 elements
The size of the array is not fixed, you can dynamically add array elements, for example
$ Name [3] = "app"; add an element
Direct access array name will get the number of elements in array, for example:
$ count = @ Name; stores the NAME's element a number in the $ count variable.
Associated arrays are a special array, each element consists of a pair of elements. Or, the associated array is a kind of
The subscript is not an array of integers, to declare an associated array, using% symbol, for example:
% Arr = (1, "One", 2, "Two", 3, "Three", 4, "FOUR");
At this time, you can use the previous value (key) to index the value:
$ one = $ arr {1}; at this time is equal to "one"
Note that the accessibility of the associated array is to use the associated array name [index number].
You can see an implementation of the associated array as a database. As with a general array, its size can also be dynamically
Section:
$ arr {5} = "five"; add a pair of data.
You can simply become an ordinary array, such as
@ X =% Arr; now @X content is x [0] = "1", x [1] = "one", ...............
Perl's operator and C language and the Gawk we introduced, including ordinary - * /% and from C language
Logical operator && (and), || (or), etc., below is a list:
- * / four calculations, pay attention to the division of Perl is floating point division
$ A% $ b A to B, for example, 3% 2 results are 1
$ 1. $ 2 segment operator, this operator takes out all the values in the middle of $ 1 and $ 2, such as 1..9 returns a table
1, 2, ......... 9. An array is usually initialized with this command, for example:
@ DEC = 1..9; @ = (1..26, 'a
'..' z '), etc..
= Assignment operator
> <> = <= ==! =
These operators are a comparative operator between numbers.
There is no special Boolean variable in Perl, but it is true that all the amount of zero is true like a C language.
0 or empty strings are false. Similar to the C language, Perl supports the following logical operators:
&& with || or!
Similarly, Perl also supports bit operations:
& & || or ^ Different or
There is also an operator similar to the C language, such as
$ i = $ j; equivalent to $ I = $ i $ J, you can also use $ I- = 5; $ I & = 12. Such an equation is $ i ; equivalent to the C language , you can use it, $ i, $ I -, - $ I can be used.
In addition to the standard equation above, Perl supports string operations, first is the comparison command between strings:
$ STR1 GT $ STR2 $ STR1 greater than $ STR2
$ STR1 LT $ STR2 $ STR1 less than $ STR2
$ STR1 GE $ STR2 $ STR1 Not less than $ STR2
$ str1 Le $ str2 $ STR1 is not greater than $ STR2
$ STR1 EQ $ STR2 $ STR1 equal to $ STR2
$ STR1 NE $ STR2 $ STR1 is not equal to $ STR2
$ STR1 CMP $ STR2 is greater than $ STR1, equal to still less than $ STR2, returns 1,0 or -1.
The above string comparison is the order of the ASCII code in the order of the character string.
Another very useful operator is a bit operator, which is used to connect two strings into one.
E.g:
$ STR1 = "string1";
$ STR2 = "String2";
$ String3 = $ str1. $ str2; at this time, $ String3 is equal to "string1string2"
2 Basic statements and functions:
#
This symbol represents the beginning of the comment.
Display strings, write files, such as
Print "Hello / N"; or Print "THE VAR IS $ I", "/ N"; Note that the variable name is automatically replaced into change
Quantity, unless you tell Perl with a / symbol clearly:
Print "/ $ I is a str";
Print file "hello / n"; writing files to the file, file is a file handle;
Split segment string, format split (/ mode /, $ string);
For example $ string = "i: am: perl";
@ List = Split (/: /, $ string);
# @List = ("i", "am", "perl")
($ A, $ B, $ C) = Split (/: /, $ string);
Delete $ array (key)
This function is used to delete a pair of records in an associated array. For example,% arr = (1, "one", 2, "two", 3, "
Three "); delete $ arr (2); After performing the above operation, the content of the% ArR becomes (1," One ", 2," Two
").
Keys (% array)
Remove all index keys in the associated array% Array. This will return an array. For example, for the above
% Arr, the result of executing @ Test = key (% arr) is @Test to become (1, 2).
VALUES (% array)
Remove all Value in the associated array% Array, also returns an array, for example for% Arr, @ test = VAL
The result of ues (% arr) is @Test to become ("One", "Two").
Reverse (@Array)
Reverse the @Array. For example, @ Test = (1, 2, 5, 3, 10), @ Other = Reverse (@test) The result is @other to become (10, 3, 5, 2, 1).
Sort (@Array)
Sort, pay attention to this sorting is sorted by the string, such as the result of @ other = sort (@other) (1,
10, 2, 3, 5).
Chop ($ String)
Deleting the last character of the string, usually used to remove the carriage return in the input string.
LENTH ($ String)
Take a string length
Substr ($ String, Offset, Length)
Take a string, which is the string of the Length length from the $ String offset offset as a substring.
Index ($ String, $ SUBSTRING)
Find $ Substring, success in $ String, return $ SUBSTRING offset in $ String, such as
Returns -1 if there is no existence.
Push (@ Array, $ String)
Add $ String at the end of @Array
POP (@Array)
Delete the end of @Array and return this element
Shift (@Array)
Delete the beginning of @Array and return this element
Join ($ String, @ array)
Add $ String in the middle of @Array and return the result
Grep (/ pattern /, @ array)
Use the regular way in @Array to find the eligible element
HEX ($ String)
Transform 16 enrollment into decimal
Rand
Generate random numbers, note that SRAND initializes random number should be performed first.
Localtime
Return to time arrays
DIE LIST
Display string and exit the program
Pack ("Format", LIST)
Convert a list into a designated binary format, such as $ string = pack ('c ", 65) At this time, $ String is equal to
ASCII's 65, ie "A"
Reverse quotation
The effect of enclosing a character in a character is to make the Perl execute system command. The reverse number used here is large.
The leftmost button of the keyboard, such as `ls` Execute the LS command.
3 use files
Using text files in Perl is very simple, just open and close files using Open and Close:
Open Opens File
Close Close the file
The OPEN function is the Open (FileHandle, $ filename), which will open $ filename file
And let the FileHandle handle point to the open file. If fails, false will be returned. Under the default, the file is
Read only ways. If you want to open the name for the $ filename file to output, use open (FileHandle
"> $ filename"). Want to add content behind a file, use Open (FileHandle, ">> $ Filena
Me "). Of course, Open (FileHandle," <$ filename ") can also be used, but this is equal to OPEN (Fi
LeHandle, "$ filename").
Read read files, format is Read (FileHandle, $ String, Length), this function points from FileHandle
Read the Length characters in the file and store it in the $ String variable. If you have to get a standard input, use the handle of Stdin.
Close (FileHandle) will turn off the file that is opened by the Open statement.
In addition to the standard approach to the READ statement, there is also a method of common use:
$ filecontent =
the amount. If you want to read a string from the console, use $ INPUT =
Below is an example:
$ filename = "test";
Open (File, "$ filename") || DIE "Can not open file !;
While ($ line =
Print "$ LINE";
}
Close (file);
This program is actually the Perl language implementation of the CAT command, the Open command opens the TEST file below the current directory,
And return the handle to the File variable, pay attention to this line usage, Perl's || (or) operation is short-circuited, such as
If Open is successful, then return a number of non-0, so this calculation will be true, so you will skip |
Things; otherwise, if the Open fails, Perl is to execute the back, so you withdrawn this program.
After opening success, Perl will get the handle of this file. The following sentence is to read the files of the file.
And it is displayed, when the file reads the end, $ line =
The E cycle ends.
Similar to the shell scripting language, Perl also has some file test operators.
-T $ file
If the file is readable, return 1, $ file is the file name.
-W $ file
If $ file can be written, return 1
-x $ file
Returns 1 if $ file can be executed
-E $ file
Returns 1 if $ file exists
-o $ file
If the user is the owner of $ file, return 1
-S $ file
Returns the size of the $ file file
-f $ file
Is it normal file?
-T $ file
Whether the text
-B $ file
Whether binary file
-M $ file
Document from update to the current date
4 process control
Perl supports flow control statements that are similar to C language:
IF and IF..EELSE:
The syntax of the IF statement is
IF (...) {
Clause;
}
Unlike the C language, even if there is only one line of programs, the curd brackets behind IF can not be omitted, this is also suitable for the back
Say other complex statements.
Similar to C language, you can also use ELSE and ELSEIF clauses:
IF (...) {
CLAUSE1;
Else {
CLAUSE2;
}
or
IF (...) {
...
}
Elseif (...) {
....
}
Else {
...
}
In addition, Perl also supports UNSS statement:
Unless (exp1) {
CLAUSE1;
}
If EXP1 is not true, the CLAUSE1 clause is executed. An else clause can also be used in this unless statement. real
In the event, this is a negative form of if ... Else statement.
While loop statement:
There are two kinds of syntax, which is to place the expression in the circular head and the tail. The first form is:
While (exp) {
Clause;
The second form is
Do {
Clause;
}
While (exp);
They are all cycled to execute Clause until EXP is not established, but one is true if the EXP expression is true at the circular head.
The other is at the end of the cycle.
Until cycle
Grammatical is Until (exp) {
Clause;
}
Clause repeatedly until EXP is established.
FOR cycle
For (initialization; continued conditions; increment) {
Cyclic body;
}
Like C, the FOR cycle is first performed, then the initialization statement is first executed, and then the loop is started, and each cycle is
All incremental expressions are called until the loop continuation conditions are no longer established. E.g
For ($ 1 = 0; $ i <10; $ i ) {
Print $ I;
}
All integers from 0 to 9 will be displayed.
Foreach $ Variable (@Array) {
Cyclic body;
}
This is similar to the shell language, which gives the contents of @Array to $ Variable and execute.
Face statement.
Jump out of the loop
There are two statements to implement special control:
Last IF is used in a loop, equivalent to Break;
Next if is equivalent to Continue.
5 text processing operation
In addition to the normal string processing statement, Perl also supports a text processing operation method, the basic format is $ STR
ING = ~ (text processing mode). This text processing mode is one of the main advantages of Perl, due to the text of Perl
The operation mode is too powerful, and only a few very basic forms can be introduced here.
First explain the concept of Pattern. Pattern is usually used with two / character clamps, with
To represent some strings with certain features, in fact, this patternj is basically GREP / EGREP
The regular expression is just a matter of functions. Commonly used:
Arbitrary string: match the string
[0-9] Match all numeric characters
[A-Z] All lowercase letters
[^ 0-9] All non-numbers
[^ a-z] all non-lowercase letters
[A-Z] All uppercase letters
^ Character starting at the beginning
Character of the string end
/ D is the same as [0-9]
/ D non-numeric characters
/ W is equivalent to [A-ZA-Z0-9]
/ W is equivalent to [^ a-za-z0-9]
/ s a blank character
/ S non-blank characters
/ d a string equivalent to numbers
/ w a string that is completely composed of numbers or characters
/ b A string that is not from English letters or numbers
/ B A string of letters or numbers as boundaries
A | B | C compliant with a string of one of A, B, and C
/ Pattern / I i representative ignore the case
[] Look for characters in line [], for example [abde] can match any of A, B, D, E
? {M} is exactly the M-specified letters
{m, n} more than M less than N specified characters
/ If you want to reference some characters with special meaning in Pattern, use / prefix
In the text arithmetic mode of Perl, the most commonly used = ~ or! ~ Operand symbols and S, TR two functions. = ~ Means matching
It uses the right mode to match the string on the left. If success, you get a true,! ~ Just opposite, represent
Do not match, for example
$ String = "chmod711cgi";
IF ($ String = ~ "/ chmod /") {
Print "Found Chmod! / N";
$ String = ~ "/ chmod /" This expression is inquiry / chmod / can match $ String because $ String
Contains CHMOD, so the if statement will be executed successfully.
TR is a string conversion function, for example:
$ String = ~ TR / A-Z / A-Z /;
Convert lowercase letters in the string into capital.
S is a serial function, for example:
$ String = ~ S / A / A /;
Change the first A to a, but also add the suffix G to indicate full replacement, for example:
$ String = ~ S / 1 / A / g;
After transform, $ String becomes ChMod7aacgi.
Note that the difference between TR and S, the multi-replacement of TR is generally one-to-one, or is the replacement of characters, and S is a string
Replace, the length can be changed, for example, if the value of $ String is chmod711cgi, then
$ String = ~ S / 1 / One / g;
Will becomes chmod7onecgi.
$ String = ~ TR / 1 / One /;
The $ String will become ChMod7OCGI.
There is also a very practical function that is variable replacement, for example:
$ String = "TEST24";
$ String = ~ S / (/ D ) / <$ 1> /;
At this time, S first searched for a string that satisfies / d (numbers), get 24, then send $ 1, then set it to <>, result is "TES
T <24>.
6 built-in variable
In addition to user-defined variables, Perl also defines some built-in variables, and they have special meaning in Perl.
Users cannot modify their meaning (some can assign), the following is a common built-in variable:
@Argv
This is an array, which represents the command line switch passing to the Perl program, such as @argv [0] is the first parameter,
@Argv [1] is the second, etc. If your Perl program name is Test, and you call it with Test Me Other
So @argv [0] is "ME", @ argv [1] is Other, and so on.
$ _ And $ 1, $ 2, .........
These parameters are used for temporary variables in text processing mode. For example, in the string mode:
$ String = "chmod711cgi"
$ String = ~ / (/ w ) / s (/ d ) /;
It seems that this second line of code is not done because it is just a matching statement. However, in fact, due to PE
RL will put the temporary variable into the $ N variable, so it will modify $ _ and $ 1, ......... variable. In this matching mode,
First / w matches a set of characters, successfully, get the chmod string, then PERL saves it to $ 1; then, / s
Match a set of blank symbols, fail; final, / d matches a set of numbers, success, get 711, Perl saves it to
$ 2.
If you do not specify the matching mode of the string variable in the matching mode, you can match.
7 Customize function
You can define your own subroutines in the Perl program. For example, the following statement defines a child process:
SUB my_proc {
Print "this is my subsrge / n";
}
Sub is a description of the sub-process, my_proc is the process name, to call this process, as long as the & symbol, for example:
IF ($ want == 1) {
& MY_PROC;
}
11.3.2 Use of Perl
The basic syntax of Perl is introduced above. To use Perl, you can of course use in interactive mode, but one
In case, we use Perl to replace the Shell script to complete the automation task. To do this, we need to write the Perl writes. Write Perl programs can be created with any text editing tool, generally always set its extension as .pl,
Although the extension is not required.
To run a Perl program, there are two ways, one is to call the Perl interpreter to read the Perl source program, for example,
We have already written a Perl program, named Test.pl, then you can execute it with the following command:
$ / Usr / bin / perl test.pl
However, in general, we prefer to perform it directly into the name directly below the program, for this,
You can add such a line on the head of the Perl program:
#! / usr / bin / perl
Note #! Symbol, it tells the shell to explain the current script, the definition here is / usr / bin /
Perl, this is the default Perl installation location. If your Perl executor is placed in another directory,
Change this line. This way, when the shell reads this file, Perl is automatically started to explain this program.
Perl's most commonly used features are used to write CGI programs, but we don't want to involve the details of the CGI program. Instead, I
We will introduce how to use Perl to perform daily management, replace the written shell script with Perl program.
Let us start from an example of my own, I often have to translate some TXT text into html text so that
Do further editing and home page release, in fact, this operation is very simple, that is, it will be charged and larger than the number, smaller than
Replace with the corresponding HTML tag, which can be done with Word or Netscape, but if the number of files involved
More or the file is relatively large, it is very difficult to use these software, so I wrote a very simple Perl script:
#! / usr / bin / perl
IF ((@ argv [0] EQ ") || (@argv [1] EQ")) {
Print "Convert InputFile OutputFile / N";
EXIT;
Open (file1, "@ argv [0]") || DIE "can not open source file! / n";
Open (file2, "> @ argv [1]") || DIE "can not open taget file! / n";
While ($ line =
$ line = ~ s / & lt / g;
$ line = ~ s /> / & gt / g;
$ line = ~ s // ^ m //g;
$ LINE = ~ S // n /
/ g;
Print file2 $ line;
}
Close (file1);
Close (file2);
This program is almost almost like a DOS batch command, first check whether the parameters are defined, then read from the input file
Taking line, replace the carriage return and two angle brackets into the corresponding HTML tag, write the corresponding file, and the task is completed. Make
With it, such as converting Test.txt to Test.html, you can do it directly:
./Convert.pl Test.txt test.html
Perl can also complete more complex operations, the most common functional extensions are network programming, such as direct use of electronics
Email, electronic news service, etc. However, we can't further introduce it to its powerful features. Friends interested in Perl can further refer to book learning programming technology.
11.4 Other Tools
There are also many automated scripting tools, such as Python, and more. In addition, for a professional system management personnel,
Familiar with C language programming is very beneficial.
I personally, I feel that if you don't want to learn the shell, you understand that Expect and Perl are enough to deal with one.
System management work. In addition, there are some powerful programmable tools, one is the PY mentioned just mentioned.
Thon, this is an object-oriented script platform if you use shell and expect, then this
Stuff may be very suitable for you.
Another very controversial product is Emacs. This editing tool is the top product of the GNU program, with development
It has developed from the original text editing program into a use LISP macro control, almost you can do a text interface.
The integrated environment of all things. The reason for opposing it is mainly its running speed almost no low configuration machine.
Because of endure, and it is also very difficult to configure. No matter how, use Emacs Sometimes it looks more professional,
Less is very characteristic of GNU