shell study-15day--shell函數

1、函數的使用

函數是一個腳本代碼塊,你能夠對它進行自定義命名,而且能夠在腳本中任意位置使用這個函數,要使用這個函數,只要使用這個函數名稱就能夠了。使用函數的好處:模塊化,代碼可讀性強。shell

1)函數建立語法

方法 1:

function name {
commands
}

注:name 是函數惟一的名稱bash

方法 2:name 後面的括號表示你正在定義一個函數

name( ){
commands
}

調用函數語法:ide

函數名 參數 1 參數 2 …模塊化

調用函數時,能夠傳遞參數。在函數中用$一、$2…來引用傳遞的參數函數

2)函數的使用

實例1:

[root@test shell]# vi fun.sh 
#!/bin/bash
function test {
    echo "test function"  
}      #以上內容定義函數
test   #調用函數
[root@test shell]# sh fun.sh 
test function
[root@test shell]#

 注:函數名的使用,若是在一個腳本中定義了重複的函數名,那麼以最後一個爲準。spa

舉例:

[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 命令來退出函數並返回特定的退出碼。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   #返回值,爲指定返回值input

注:狀態碼的肯定必須要在函數一結束就運行 return 返回值;狀態碼的取值範圍(0~255)。 it

[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]#

 注:return 只是在函數最後添加一行,而後返回數字,只能讓函數後面的命令不執行,沒法強制退出整個腳本。exit 整個腳本就直接退出。io

4)將函數賦值給變量

函數名至關於一個命令。

[root@test shell]# cat fun.sh 
#!/bin/bash
function test {
    read -p "輸入一個整數:" int
        echo $[ $int+1 ]
}
 
sum=$(test)
echo "result is $sum"
[root@test shell]# sh fun.sh 
輸入一個整數:1
result is 2
[root@test shell]#

5)函數參數傳遞

實例1:經過腳本傳遞參數給函數中的位置參數$1

[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]#

實例2:調用函數時直接傳遞參數

[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]#

實例3:函數中多參數傳遞

[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)函數中變量處理

函數使用的變量類型有兩種:

局部變量、全局變量。

全局變量,默認狀況下,你在腳本中定義的變量都是全局變量,你在函數外面定義的變量在函數內也能夠使用。

[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]#

我的公衆號:

image.png

相關文章
相關標籤/搜索