歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Unix shell自定義函數的簡介及使用

Unix shell自定義函數的簡介及使用

日期:2017/3/1 16:50:14   编辑:SHELL編程
Unix shell自定義函數的簡介及使用 一、無參函數 沒有參數的函數,直接調用實現某些功能。 函數編寫在腳本中,與其他命令一起存儲,但是函數必須定義在腳本的最開始部分; 也就是說,包含函數的腳本中,所有的函數都得定義在腳本的最開始部分; 然後在定義函數之後調用或者在其他腳本中引用這些定義的函數。 www.2cto.com 實例1、下面是一個簡單的自定義函數,求1到10的和: [plain] pg no_param_test #!/bin/ksh # 測試無參自定義函數 www.2cto.com # author:_yeeXun # date :2013-3-4 8:37:29 no_param_test() { SUM=0 #for i in { 1..10 } for i in 1 2 3 4 5 6 7 8 9 10 do echo $i SUM=`expr $SUM + $i` i=`expr $i + 1` if [ $i -eq 11 ]; then echo "Sum:$SUM" fi done } no_param_test # EOF 執行腳本: sh no_param_test 1 2 3 4 5 6 7 8 9 10 Sum:55 二、有參函數 帶參數的自定義函數,在高級語言(如,C,JAVA,C#等)中,參數都是在函數後面的括號中; 而shell中則在函數體內檢查參數個數。在調用函數的時候,直接在後面跟著參數即可。 無論參數有多少個,都緊跟在函數名後面。 1、單個參數 實例2、腳本如下: [plain] pg direc_check #!/bin/ksh is_directory() { _DIR_NAME=$1 # check params if [ $# -lt 1 ]; then echo "is_directory:I need a directory name to check" return 1 fi # check if [ ! -d $_DIR_NAME ]; then return 1 else return 0 fi } error_msg() { echo "**********" echo $@ echo "**********" return 0 } echo -n "Enter destination directory:" read DIR if is_directory $DIR then echo "$DIR is a directory and exists in $PWD" else error_msg "$DIR does not exist ... creating it now" mkdir $DIR > /dev/null 2>&1 if [ $? -ne 0 ]; then error_msg "Could not create directory::chech it out!" exit 1 else : fi fi # author:_yeeXun # date :2013-3-2 15:27:57 # EOF 這裡函數中# check params部分就是在做參數檢查,這裡$#表示輸入的參數個數。 下面是執行這個腳本: sh direc_check Enter destination directory:test ********** test does not exist ... creating it now ********** 再次執行時候,輸入同樣的名稱,則會提示此目錄已經存在。 sh direc_check Enter destination directory:test test is a directory and exists in /usr/b4nx/user/ytcclb 我們在添加一個目錄,通過awk語言查看當前目錄下通過此腳本創建的目錄: [plain] ls -l | awk '{if($0~/^d/) print $0}' drwxr-xr-x 2 b4nx group 512 Mar 2 15:32 call_fun_test drwxr-xr-x 2 b4nx group 512 Mar 2 15:04 test 2、多個參數 不管是單個參數,還是多個參數,都通過檢查判斷,這裡$#表示輸入的參數個數; $1表示輸入的第一個參數,$2表示第二個,$3表示第四個,以此類推。 下面是一個截取字符串的函數: 實例3、pg chop [plain] #!/bin/ksh # chop # to call:chop string how_many_chars_to_chop chop() { _STR=$1 _CHOP=$2 # awk's substr starts at 0,wo need to increment by one CHOP=`expr $_CHOP + 1` # check we have two params if [ $# -ne 2 ]; then echo "check_length:I need a string and how many characters to chop" return 1 fi # check the length of the string first _LENGTH=`echo $_STR | awk '{print length($0)}'` if [ "$_LENGTH" -lt "$_CHOP" ]; then echo "The length of the string is short" return 1 fi echo $_STR | awk '{print substr($1,'$_CHOP')}' } # call the function echo -n "Enter the string:" read STR echo -n "Enter the start position:" read LEN chop $STR $LEN # author:_yeeXun # date :2013-3-2 14:17:34 # EOF 這個腳本中,定義了一個函數chop截取字符串,2個參數,分別用$1,$2表示; $1表示被截取的字符串,$2表示截取的起始位置。執行如下: sh chop Enter the string:string123 Enter the start position:5 ng123 sh chop Enter the string:_yeexunInCSDN Enter the start position:10 CSDN 三、函數返回值 函數的返回值表示執行函數的一個狀態, 1、return 0:正常返回。 2、return 1:返回錯誤。 3、return :最後的命令執行狀態決定返回值,可使用$?檢測。 四、函數調用 1、同腳本 腳本中,函數必須在腳本一開始就定義,定義之後才可調用。 同一腳本中,在頂部定義函數,定義完後再調用,如實例1、實例2、實例3。 2、腳本間 可以將這些函數定義在同一個腳本中,作為公共使用的函數腳本,然後再需要調用的腳本中引用此腳本即可。 引用方式如下:<dot><space><directory><script> 實例4、函數調用 — 跨腳本 函數腳本: [plain] pg direc_check.sh #!/bin/ksh is_directory() { _DIR_NAME=$1 # check params if [ $# -lt 1 ]; then echo "is_directory:I need a directory name to check" return 1 fi # check if [ ! -d $_DIR_NAME ]; then return 1 else return 0 fi } error_msg() { echo "**********" echo $@ echo "**********" return 0 } # 多行注釋方式::<<' ... ' :<<' echo -n "Enter destination directory:" read DIR if is_directory $DIR then echo "$DIR is a directory and exists in $PWD" else error_msg "$DIR does not exist ... creating it now" mkdir $DIR > /dev/null 2>&1 if [ $? -ne 0 ]; then error_msg "Could not create directory::chech it out!" exit 1 else : fi fi ' # author:_yeeXun # date :2013-3-2 15:27:57 # EOF 調用此腳本中函數的腳本: [plain] pg fun_test #!/bin/ksh # call the function outside the script is_drectory # <dot><space><directory with functions><script with functions> . $HOME/user/ytcclb/direc_check.sh echo -n "Enter destination directory:" read DIR if is_directory $DIR then echo "$DIR is a directory and exists in $PWD" else error_msg "$DIR does not exist ... creating now" mkdir $DIR > /dev/null 2>&1 if [ $? -ne 0 ]; then error_msg "Could not create directory::check it out" exit 1 else : fi fi # author:_yeeXun # date :2013-3-2 15:49:12 # EOF 當輸入一個已存在的目錄時候: sh fun_test Enter destination directory:test test is a directory and exists in /usr/b4nx/user/ytcclb 輸入不存在的目錄名: sh fun_test Enter destination directory:dir_test ********** dir_test does not exist ... creating now ********** 查看當前目錄下所有目錄名: [plain] ls -l | awk '{if($0~/^d/) print $0}' drwxr-xr-x 2 b4nx group 512 Mar 2 15:32 call_fun_test drwxr-xr-x 2 b4nx group 512 Mar 4 14:16 dir_test drwxr-xr-x 2 b4nx group 512 Mar 2 15:04 test --the end--
Copyright © Linux教程網 All Rights Reserved