標籤:shell function
1. 函數
shell的函數(function),用於自訂一段程式段。用於簡化代碼。文法:
funtion fname() { //do something}
shell是自上而下,由左而右執行的。所以函數的定義需要在調用的前面。例如:
#!/bin/bash# author : yonggangfunction print_it(){ echo -n "Your choice is : " }case $1 in "one") print_it; echo $1; ;; "two") print_it; echo $1; ;; "three") print_it; echo $1; ;; *) echo "Usage $0 (one|two|three)" ;;esac
執行:
[[email protected] sh]$ sh func.sh twoYour choice is : two[[email protected] sh]$ sh func.sh oneYour choice is : one[[email protected] sh]$
2. 函數參數傳遞
function也擁有內建變數,與shell script類似。$# 參數個數$1 第一個參數$2 第二個參數...[email protected] 所有參數看下面例子:
#!/bin/bash# author : yonggangfunction print_param(){ echo "paramter number : " $# echo "first paramter : " $1 echo "second paramter : " $2 echo "all paramter : " [email protected]}print_param one two three
運行:
[[email protected] sh]$ sh func.sh paramter number : 3first paramter : onesecond paramter : twoall paramter : one two three[[email protected] sh]$
地址:http://blog.csdn.net/yonggang7/article/details/40679111
shell函數功能