definition of shell and function
1. Linux Shell functions
form a set of command sets or statements into an available block, These statement blocks are called functions.
2. composition of Shell functions
Functions Name: Function names, note that the function name in a script is unique, otherwise it will cause the calling function to be disorganized.
function Body: a collection of functions internal commands that implement a business function.
3. format of Shell function definition
function function name () # function can be omitted, note () inside without any parameters.
{
Command 1
Command 2
. . .
}
Examples of function use methods
1. function definition and reference instance one: The simplest definition and reference
[Email protected] myscript]$ sh func_1.sh
Walkthrough function Definition and Reference method :
Welcome,now time is Wed 16:01:58 CST 2015
Actual environment operation:
Execution Result:
2. function definition and reference instance two: passing parameters to a function walkthrough
passing arguments to a function is like using a special variable $1,$2...$9 in a generic script , after the function obtains the argument, it is best to reset the variable to save the argument within the function.
Program function: Pass a variable value to the function, perform the related operation on the value of the variable, and output the result.
[Email protected] myscript]$ sh func_2.sh
The method of passing the arguments inside the function (note the method of receiving the variable)
Number of arguments conforming to function requirements
10+90=100
Actual environment operation:
Execution Result:
3. function files and script files are not in the same file, how can I call this function?
program function: Enter the path of a file, and then print out the file's MD5 summary information.
[[email protected] myscript]$ Cat func_3.sh # function File
[[Email protected] myscript]$ SH func_call.sh # script file
Please enter a file name : test1.sh
Test1.sh 's MD5 Summary is : 5615ac3ce36ec58748a80649c3599d88 test1.sh
Please enter a file name : test2.sh
Test2.sh 's MD5 Summary is : 4446b536dff610ea440663147914c52b test2.sh
Please enter a file name : ttt.sh
Ttt.sh 's MD5 Summary is : 5615ac3ce36ec58748a80649c3599d88 ttt.sh
Notice how the function is imported:
. whitespace file name
Actual environment operation:
Execution Result:
Original link:http://www.maiziedu.com/wiki/linux/array/
Linux operating system shell and functions