Tag:shell function
function definition: 1, no return value #function为关键字, function_name for Function name function function_name () {com Mand1 Command2 ...} Omit the keyword FUNCTION, the effect is the same as function_name () {command ...} Example: 1, simple function declaration and call #!/bin/bash function SayHello () { echo "Hello world!" SayHello Note: 1, SayHello call without (), SayHello () such a call will be an error. 2. If the call is declared again, there cannot be another statement between the call and the declaration 2, the number of rows of the computed file #!/bin/bash if [[$#-lt 1]];then echo "Please input a filename." return fi file=$1 countline function countline () {Local line=0 WHile read do let "line++"; Done < $file echo "$file have $line lines." } 2, the return value of the function returns the primary way to get the return value is $? Example: #检测文件是否存在 #!/bin/bash file=$1 Check if [$? -eq 0];then echo "$file exists." else echo "$file doesn ' t exist." fi function Check () {if [-e $file];then return 0 else return 1 fi} 3, function with parameter 1, positional parameter This is not the same as the high-level language, in the function declaration does not specify the parameters, but directly in the function body to use, the call when the direct parameters can be passed: 1, the detection of the existence of the file #!/bin/bash Check #这里不再使用file变量 if [$?-eq 0];then echo "$ exists." else echo "$ doesn ' t exist." fi function Check () {if [-E $];then #函数体里的参数 return 0 Else return 1 fi } 2, calculates the sum of two #!/bin/bash function add () { Local Tmp=0 i=$1 j=$2 Let "Tmp=i+j" return $tmp} add $ echo "$?" 2. Specify positional parameter values use the set built-in command to give the script the value of the positional parameter (also known as reset). Once the value of the passed parameter is set by using set, the script ignores the positional parameters passed in at run time (in fact, the value of the positional parameter is reset by set) Example: #!/bin/bash Set 1 2 3 4 Count=1 for Var in [email protected] do echo "H Ere \$ $count is $var ' let ' count++ "done Note: No matter how many parameters are reset to four parameter input Number. Such as:. ./function03.sh a b c d E F result: here \ 1 Here are 2 here $ is 3 here $4 is 4 Note: What's the point? 3, moving position parameter review: Shift, in the case of no parameters, this command can move the position parameter one bit to the left. Example: #!/bin/bash until [$#-eq 0] Do echo "Now \$1 is:$1,total paramert is:$#" Shift done Note: The position parameter is utilized, [email protected]/$*: All Parameters $: $n: The nth parameter, when n is greater than 10 o'clock, to enclose N in () to the "." Script itself. When you execute a script, the file name that is returned when you execute the script with bash is returned when you execute it with./scriptname./scriptname $#: All parameter extensions specify the number of left-shifted digits, shift N: #!/bin/bash echo "$" until [$#-eq 0] Do echo ' now \$1 is:$1,total paramert is:$# ' Shift 2 Done NOTE: If you specify an odd number of parameters when you enter a command, the script will fall into a dead loop. 4, function library in order to distinguish the common function, in practice, it is suggested that the library function use the underscore (_) to begin loading the library function: 1, using the "point" (.) Command 2, using the Source command example: 1, building a function library is actually writing a script file that is a function. Example: Building a library lib01.sh function _checkfileexist () { If [-E $];then echo "file:$1 exists" else echo "file:$1 doesn ' t exists" fi } 2, use #!/bin/bash #source./lib01.s H. ./lib01.sh _checkfileexist system function Library:/etc/init.d/functions (my system is Ubuntu, I don't see To this file) a 27 function
Shell Learning note Eight (function)