Functions in shell are similar to methods in Java. script code is placed in the function and can be run anywhere to reduce a large amount of repeated code.
Understanding Shell functions involves the following aspects: function creation, function calling, variables, return values, and parameters.
1. Create and call functions.
Basic Structure: There are two methods with and without functions,
Function Hello () {}and Hello (){}
For example:
#! /Bin/shfunction Hello () {echo "this is a day: 'date' "} echo '======== 'helloecho' =======' # execution result ====== this is a day: fri Sep 28 11:56:00 CST 2012 ======
Note: 1. The function must be created before calling. 2. Do not repeat the function name; otherwise, the first defined function will be overwritten.
2. Return Value
First, let's talk about the default exit status. $? Is Used in the script? . Indicates the exit status of the last command to be executed. 0 indicates successful, and a non-0 indicates failed.
2.1、$?
#! /Bin/shfunction Hello () {echo "this is a day: 'date'" CD/qwer} echo "State: $? "Echo '====== 'Hello echo" State: $? "Echo '=' # execution result: State: 0 = This is a day: Fri Sep 28 12:12:31 CST 2012. /Fun. SH: Line 5: CD:/qwer: no such file or directorystate: 1 ========
As shown above: two $? 0 and 1 respectively. Let's look at the next program:
#! /Bin/shfunction hello () {echo "this is a day: 'date'" cd/qwer} echo "state: $? "Echo '= 'Hello echo" test... "echo" state: $? "Echo '=' # execution result: state: 0 = this is a day: Fri Sep 28 12:19:28 CST 2012. /fun. sh: line 5: cd:/qwer: No such file or directorytest .. test... state: 0 ========
Now I can deeply understand $? The last invocation status of the shell command. When other commands are executed after the function is executed, then $? It indicates the execution status of other commands.
About $? There is also fun:
#! /Bin/shfunction hello () {cd/qwer echo "this is a day: 'date'"} echo "state: $? "Echo '====== 'Hello echo" state: $? "Echo '======' # execution result: state: 0 ========. /fun. sh: line 4: cd:/qwer: No such file or directorythis is a day: Fri Sep 28 12:26:43 CST 2012 state: 0 ======
Well, the default exit status is not very good.
2.2 return
Return can only return a single integer. An error is returned for a string script.
See the following program. Compare the value entered by the user with 100, and return different values.
Read-p "please input:" valuetest () {if [$ value-lt 100] then echo "<100" return-1 elif [$ value-eq 100] then echo "= 100" return 0 else echo "> 100" return 1fi} testecho "result value is: $? "# Execution result: please input: 999> 100 result value is: 1
That is, the return value is composed of $? Accept.
==========================================
If, after the end of the above script if statement, the return 1000
Final program output,
Please input: 999
> 100
Result value is: 1
Why is it 1? Sometimes 255 ,???
==========================================
Another method is to use function output. The output can be captured and put into shell variables.
Ceo () {read-p "please input a value:" valueecho $ [$ value * 2]} result = 'ceos 'echo "the result is: $ result "# execution result: please input a value: 999the result is: 1998
Note that the result is "ceo'
==========================================
Or the above script to replace the output:
Echo $ [$ vale]
Echo $ [$ value * 2]
The output is: the result is: 0.
Do not understand?
==========================================
3. Passing Parameters
The passing of shell script function parameters can be represented by standard parameter environment variables.
The following script outputs different parameters based on the number of input parameters.
#! /Bin/shfunction param () {if [$ #-lt 2] then echo "param count <2" elif [$ #-gt 2] then echo "param count> 2" else echo "param count = 2 "fi} param 1 2 3 # execution result: param count> 2
Note that the function cannot directly use the script parameters. The function uses dedicated parameter environment variables for its own parameters.
That is to say, when running a script, the parameter functions passed in through the command line cannot be directly used, and must be passed in manually.
See the following example:
Function useMainPram () {echo "----" echo $1 echo $2 echo "----"} echo $1 echo $2 useMainPram # name the file param. sh. /param. sh 98 99 execution result: 9899 --------
No value is printed in the function. In the adjustment script, the function call is: useMainPram $1 $2.
The same command execution result is displayed correctly.
4. Variables
Variables are mainly scope issues. variables defined in the script (not inside the function) can be used anywhere and belong to global variables.
The local variable can be defined inside the function, and the scope is only within the function.
B = 100 function tt () {local B = 10 result = $ [$ B + 5]} ttecho $ resultecho $ B # execution result: 15100
5. library files
Functions can be used inside the script. If multiple scripts use the same function, they must use the library file.
First, create a library file named ku. Note that there is no suffix Name:
function useMainPram(){ echo "----" echo $1 echo $2 echo "----"}
Then, call it in the script file. The Script Name Is param. sh:
../Kuecho $1 echo $2 useMainPram $1 $2 # execution result./param. sh: 9899----9899 ----
Note that the format of the imported files
There is a space at the end of the vertex, and then the execution library file.
It's almost the same here, and the basic operations of the function are just like this.