Shell allows a group of command sets or statements to form an available block. These blocks are called shell functions. Function body. The title is the function name. The function body is a set of commands in a function. The title name should be unique; otherwise, the results will be obfuscated because the script will
S h e l allows a group of command sets or statements to form an available block, which is called the s h e l function.
Function body.
The title is the function name. The function body is a set of commands in a function. The title name should be unique; if not, it will be confused
Because the script first searches for the corresponding s h e l called by the function before viewing the called script.
The format of the defined function is:
Function name ()
{
Command 1
...
}
Or
Function name (){
Command 1
...
}
Both methods are feasible. If you want to, you can add the keyword f u n c t I o n before the function name, depending on the user.
F u n c t I o n function name ()
{...
}
You can think of a function as a piece of code in a script, but there is a major difference. When the function is executed, it retains the current
S h e l and memory information. In addition, if you execute or call another piece of code in a script file, a separate
S h e l, so all existing variables defined in the original script are removed.
Functions can be stored in the same file as a piece of code, or in a separate file that only contains functions. Function
It does not need to contain many statements or commands, or even only one e c h o statement, depending on the user.
All functions must be defined before use. This means that the function must be placed at the beginning of the script until the s h e l interpreter
It can be used only when it is first discovered. Only the function name can be used to call a function.
Passing parameters to a function is like using special variables $1, $2... $9 in a general script. after obtaining the passed parameters,
Return the original parameter to the s h e l Script. Therefore, you are advised to reset the variable in the function to save the passed parameter. In this case
If the number of variables is incorrect, you can quickly track them through localized variable names. Call
Start by converting the following dashes and add variable names, for example, _ f I l e n a m e or _ f I l e n a m e.
When the function is processed or you want the function to be returned based on a test statement, you can perform the following two operations:
1) let the function normally run to the end of the function, and then return to the control part of the function called in the script.
2) use r e t u r n to return the next statement called by the function in the script. the returned value can be included. 0 indicates no error, and 1 indicates an error.
This is optional and is similar to the final state Command report example. The format is:
R e t u r n is returned from the function, and the return value is determined by the final state command.
Return 0: No error returned.
Return 1 error returned
You can use the final state command directly after the script calls the function statement to test the return value of the function call.
[Root @ localhost huangcd] # cat test.txt
#! /Bin/bash
Hello ()
{
Echo "hello there today's date is 'date '"
}
Hello
If [$? = 0]
Then echo "OK"
Else
Echo "wrong"
Fi
[Root @ localhost huangcd] # sh test.txt
Hello there today's date is December 11, 2013 Wednesday 09:13:31 CST
OK
When you collect some frequently used functions, you can put them into the function file and load the file into s h e l.
The file header should contain statements #! /B I n/s h, the file name can be arbitrarily selected, but it is best to have some practical contact with the relevant task. Example
For example, f u n c t I o n s. m a I n.
Once the file is loaded into s h e l, you can call the function in the command line or script. You can use the s e t command to view all definitions.
. The output list contains all functions loaded into s h e l.
To change the function, run the u n s e t command to delete the function from s h e l, even though u n s e t deletes the function to facilitate this function.
Numbers are not available for s h e l or scripts, but they are not actually deleted. After the modification, re-load the file. Some
S h e l recognizes the changes and does not need to use the u n s e t command. However, for security reasons, it is best to use the u n s e t command when changing a function.
Next we will create a function file for the inclusion function and load it into s h e l, test it, make changes, and load it again.
The function file name is f u n c t I o n s. m a I n. the content is as follows:
The above script has been used before and is now converted into a function. This is the front end of a basic f I n d command. If
If no parameter is added, the function returns 1, indicating that an error occurs. Note that the actual function name is used in the error statement, because $0 is used here,
S h e l will only return s h-information, because the file is not a script file. This type of information is of little help to users.
[Root @ localhost huangcd] # cat functions. main
#! /Bin/bash
Findit (){
If [$ #-lt 1]
Then echo "usage: findit file"
Retrun 1
Fi
Find/-name $1-print
}
The positioning file format is:
./P a t h n a m e/f I l e n a m e
Now that the file has been created, you need to load it into s h e l. try typing:
$. Functions. main
If file not found is returned, try again:
$./Functions. main
This is <点> <空格> <斜线> <文件名> , Now the file should have been loaded into s h e l. If the error persists
Check whether the complete path name is entered.
[Root @ localhost huangcd] #. functions. main
Use the s e t command to ensure that the function is loaded. The s e t command displays all loading functions in s h e l.
[Root @ localhost huangcd] # set
BASH =/bin/bash
BASH_ARGC = ()
BASH_ARGV = ()
BASH_LINENO = ()
Tmpid = 0
Findit ()
{
If [$ #-lt 1]; then
Echo "usage: findit file ";
Return 1;
Fi;
Find/-name $1-print
}
To execute a function, simply enter the function name. Here is the f I n d I t function with a parameter. the parameter is a system
System file.
[Root @ localhost huangcd] # findit huangcd
/Home/huangcd
[2] + Stopped find/-name $1-print
[Root @ localhost huangcd] # findit
Usage: findit file
Now, make some changes to the function. First, delete the function so that it is not available to s h e l. Use the u n s e t command to complete this task
Yes. When deleting a function, the format of the u n s e t command is unset function_name.
[Root @ localhost huangcd] # unset findit
Edit the function f u n c t I o n s. m a I n and add the f o r loop so that the script can read multiple parameters from the command line. Post-modification letter
The script is as follows:
[Root @ localhost huangcd] # cat functions. main
#! /Bin/bash
Findit (){
If [$ #-lt 1]; then
Echo "usage: findit file"
Return 1
Fi
For loop
Do
Find/-name $1-print
Done
}
Locate the function again
[Root @ localhost huangcd] #. functions. main
Run the s e t command to check whether it is loaded. we can find that s h e l correctly interprets the f o r loop to accept all input parameters.
[Root @ localhost huangcd] # set
Tmpid = 0
Findit ()
{
If [$ #-lt 1]; then
Echo "usage: findit file ";
Return 1;
Fi;
For loop in "$ @";
Do
Find/-name $1-print;
Done
}
Run the modified f I n d I t function and enter two parameters:
[Root @ localhost huangcd] # findit huangcd
/Home/huangcd
/Var/spool/mail/huangcd
/Var/run/sudo/huangcd
/Home/huangcd
/Var/spool/mail/huangcd
/Var/run/sudo/huangcd
You have new mail in/var/spool/mail/root
Now that you have learned the basic usage of the function, use it to do some work. Functions can save a lot of programming
Time, because it is reusable.
Only letters are required. If you do not need a function to implement this, you need to write a large number of scripts. Use functions
To delete repeated scripts. Test characters in a w k language. The following is a test function for obtaining lowercase or upper-case characters.
First, set the variable $1 to a meaningful name, and then use a w k to test that the entire transfer record only contains letters.
Output (1 is non-letter, empty is successful) is saved in variable _ l e t e r s o n ly.
Then run the variable test. if it is null, the test is successful. if there is a value, the test is an error. Based on this test, return
Code is then executed. When testing the function call part of the script, using the return value will make the script clear and understandable.
Use the I f statement format to test the function:
If an error occurs, write a function to report the error to the screen.
Function n a m e r o r is used to display all invalid input errors. Use special variable $ @ to display all parameters. here it is changed
F n a m e and s n a m e. The script is as follows:
[root@localhost huangcd]# cat func2#!/bin/bashchar_name(){# check if $1 indeed contain only characters a-z,A-Z_LETTERS_NOLY=$1_LETTERS_ONLY=`echo $1|awk '{if($0~/[^a-zA-Z]/) print "1"}'`if[ "$_LETTERS_ONLY" != "" ]then return 1else return 0fi}name_error(){ echo "$@ contains errors,it must contain only letters"}while:do echo -n "what is your first name:" read F_NAME if char_name $F_NAME then break else name_error $F_NAME fidonewhile:do echo -n "what is your surname:" read S_NAME if char_name $S_NAME then break else name_error $S_NAME fidone