shell study-15day--shell函数

1. Use of functions

A function is a script code block, you can name it yourself, and you can use this function anywhere in the script. To use this function, just use this function name. The benefits of using functions: modularity, code readability.

( 1) Function creation syntax

Method 1:

function name {
commands
}

Note: name is the unique name of the function

Method 2: The brackets after name indicate that you are defining a function

name( ){
commands
}

Call function syntax:

Function name parameter 1 parameter 2…

When calling a function, you can pass parameters. Use $1, $2... to refer to the passed parameters in the function

( 2) Use of functions

Example 1:

[root@test shell]# vi fun.sh 
#!/bin/bash 
function test { 
    echo "test function"   
} #The  above content defines function 
test 
#call function [root@test shell]# sh fun.sh  
test function 
[root @test shell]#

 Note: For the use of function names, if repeated function names are defined in a script, the last one shall prevail.

For example:

[root@test shell]# sh fun.sh 
test function
[root@test shell]# vi fun.sh 
#!/bin/bash
function test {
    echo "test function one"
}
function test {
    echo "test function two"
}
test
[root@test shell]# sh fun.sh 
test function two
[root@test shell]#

( 3) Return value

Use the return command to exit the function and return a specific exit code.

[root@test shell]# vi fun.sh 
#!/bin/bash
function test {
    echo "test function one"
    ls /home
    return 2
}
test
[root@test shell]# sh fun.sh 
test function one
1.txt  YDSOC_C4I_SYSLOG  YDSOC_C4I_SYSLOG_20201009.tar.gz  aa  bb  cc  client111.crt  client111.csr  client111.key  test1
[root@test shell]# echo $?

2 #Return value, the specified return value

Note: The determination of the status code must be executed at the end of the function to return the return value; the value range of the status code (0~255). 

[root@test shell]# vi fun.sh 
#!/bin/bash
function test {
    echo "test function one"
    ls /home
    return 2
    ls /root
}
test
[root@test shell]# sh fun.sh 
test function one
1.txt  YDSOC_C4I_SYSLOG  YDSOC_C4I_SYSLOG_20201009.tar.gz  aa  bb  cc  client111.crt  client111.csr  client111.key  test1
[root@test shell]# echo $?
2
[root@test shell]#

 Note: return just adds a line at the end of the function, and then returns the number. It can only prevent the command after the function from being executed, and cannot force the entire script to exit. exit The entire script exits directly.

( 4) Assign function to variable

The function name is equivalent to a command.

[root@test shell]# cat fun.sh  
#!/bin/bash 
function test { 
    read -p "Enter an integer:" int 
        echo $[ $int+1] 
} 
 
sum=$(test) 
echo "result is $ sum" 
[root@test shell]# sh fun.sh  
input an integer: 1 
result is 2 
[root@test shell]#

( 5) Function parameter transfer

Example 1: Passing parameters to the positional parameter $1 in the function through the script

[root@test shell]# cat fun.sh 
#!/bin/bash
function test {
        rm -rf $1
}
test $1
[root@test shell]# touch 1.txt
[root@test shell]# ls
1.txt  fun.sh
[root@test shell]# sh fun.sh 1.txt 
[root@test shell]# ls
fun.sh
[root@test shell]#

Example 2: Pass parameters directly when calling a function

[root@test shell]# touch /home/test.txt
[root@test shell]# vi fun.sh 
#!/bin/bash
function test {
        rm -rf $1
}
test /home/test.txt
[root@test shell]# ls /home
test.txt
[root@test shell]# sh fun.sh 
[root@test shell]# ls /home
[root@test shell]#

Example 3: Passing multiple parameters in a function

[root@test shell]# vi fun.sh 
#!/bin/bash
function test {
        rm -rf $1
        rm -rf $2
}
test /home/test1 /home/test2
[root@test shell]# touch /home/test{1,2}
[root@test shell]# ls /home
test1  test2
[root@test shell]# sh fun.sh 
[root@test shell]# ls /home
[root@test shell]#

( 6) Variable processing in the function

There are two types of variables used by functions:

Local variables, global variables.

Global variables. By default, the variables you define in the script are global variables, and the variables you define outside the function can also be used inside the function.

[root@test shell]# cat fun.sh 
#!/bin/bash
function test {
        num=$[var*2]
}
read -p "input a num:" var
test
echo the new value is: $num
[root@test shell]# sh fun.sh 
input a num:1
the new value is: 2
[root@test shell]#

Personal public number:

image.png

Guess you like

Origin blog.51cto.com/13440764/2575392