1.機能の使用
関数はスクリプトコードブロックであり、自分で名前を付けて、スクリプト内の任意の場所でこの関数を使用できます。この関数を使用するには、関数名を使用するだけです。機能を使用する利点:モジュール性、コードの読みやすさ。
( 1)関数作成構文
方法1:
関数名{ コマンド }
注:nameは関数の一意の名前です
方法2:名前の後の括弧は、関数を定義していることを示します
name(){ コマンド }
関数構文の呼び出し:
関数名 パラメーター1パラメーター2…
関数を呼び出すときに、パラメーターを渡すことができます。使用する機能に渡されたパラメータを参照するために$ 1、$ 2 ...
( 2)機能の使用
例1:
[root @ test shell] #vi fun.sh #!/ bin / bash function test { echo "test function" } # 上記の内容は、function test #call function [root @ test shell] #sh fun.sh test function [root @テストシェル]#
注:関数名を使用する場合、スクリプトで繰り返し関数名が定義されている場合は、最後の関数名が優先されます。
例えば:
[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コマンドを使用して関数を終了し、特定の終了コードを返します。
[ルート@テストシェル]#viのfun.sh #!/ binに/、bashの 機能テスト{ エコー"テスト機能1" LS /家庭 復帰2 } テスト [ルート@テストシェル]#shのfun.shの テスト機能1 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#戻り値、指定された戻り値
注:ステータスコードの決定は、戻り値、つまりステータスコードの値の範囲(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]#
注:returnは、関数の最後に行を追加してから番号を返します。これは、関数の実行後のコマンドを防ぐことしかできず、スクリプト全体を強制的に終了させることはできません。exitスクリプト全体が直接終了します。
( 4)変数に関数を割り当てる
関数名はコマンドに相当します。
[root @ test shell] #cat fun.sh #!/ bin / bash function test { read -p "Enter a integer:" int echo $ [$ int + 1] } sum = $(test) echo "result is $ sum " [root @ test shell] #sh fun.sh 入力整数:1 結果は2 [root @ test shell]#
( 5)機能パラメータの転送
例1:スクリプトを介して関数内の位置パラメーター$ 1にパラメーターを渡す
[ルート@テストシェル]#猫fun.shの #!/ binに/、bashの 機能テスト{ RM -rf $ 1 } テスト$ 1 [ルート@テストシェル]#タッチ1.TXT [ルート@テストシェル]#LSを 1.TXT楽しいです.SH [試験シェル@ルート]#SH fun.sh 1.TXT [試験シェル@ルート]#LS fun.sh [試験シェル@ルート]#
例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)関数内の可変処理
関数で使用される変数には、次の2つのタイプがあります。
ローカル変数、グローバル変数。
グローバル変数デフォルトでは、スクリプトで定義する変数はグローバル変数であり、関数の外部で定義する変数は、関数の内部でも使用できます。
[root @ test shell] #cat fun.sh #!/ bin / bash function test { num = $ [var * 2] } read -p "input a num:" var test echo新しい値は次のとおりです:$ num [root @test shell] #sh fun.sh input a num:1 新しい値は次のとおりです:2 [root @ test shell]#
個人公開番号: