函數由兩部分組成:
標題是函數名。函數體是函數內的命令集合。標題名應該唯一;如果不是,將會混淆結,因為腳本在查看調用腳本前將首先搜索函數調用相應的 s h e l l。
定義函數的格式為:
- 函數名()
- {
- 命令1
- . . .
- }
- 或者
- function 函數名()
- { ...
- }
函數名()
{
命令1
. . .
}
或者
function 函數名()
{ ...
}
兩者方式都可行。如果愿意,可在函數名前加上關鍵字function,這取決于使用者。
創建函數文件
下面創建包容函數的函數文件并將之載入shell,進行測試,再做改動,之后再重新載入。
函數文件名為functions.main,內容如下
- #!/bin/sh
- #functions.main
- #
- #findit: this is front end for the basic find command
- findit() {
- #findit
- if [ $# -lt 1 ]; then
- echo "usage : findit file"
- return 1;
- fi
- find . -name $1 -print
- }
#!/bin/sh
#functions.main
#
#findit: this is front end for the basic find command
findit() {
#findit
if [ $# -lt 1 ]; then
echo "usage : findit file"
return 1;
fi
find . -name $1 -print
}
定位文件
定位文件格式為:
. /pahname/filename
現在文件已經創建好了,要將之載入shell,試鍵入:
$. functions.main
如果返回信息file not found,再試:
$. /functions.main
此即<點> <空格> <斜線> <文件名>,現在文件應該已載入shell。如果仍有錯誤,則應該仔細檢查是否鍵入了完整路徑名
檢查載入函數
使用set命令確保函數已載入。set命令將在shell中顯示所有的載入函數。
- /home/l/g/tomotoboy/function >. function.main
- /home/l/g/tomotoboy/function >set
- ……
- _=function.main
- findit ()
- {
- if [ $# -lt 1 ]; then
- echo "usage : findit file";
- return 1;
- fi;
- find . -name $1 -print
- }
/home/l/g/tomotoboy/function >. function.main
/home/l/g/tomotoboy/function >set
……
_=function.main
findit ()
{
if [ $# -lt 1 ]; then
echo "usage : findit file";
return 1;
fi;
find . -name $1 -print
}
執行shell函數
要執行函數,簡單地鍵入函數名即可。這里是帶有一個參數的 findit函數,參數是某個文件
- /home/l/g/tomotoboy/function >cd .
- /home/l/g/tomotoboy/function >cd ..
- /home/l/g/tomotoboy >findit sed.txt
- ./testdirec/sed.txt
- ./sed.txt
/home/l/g/tomotoboy/function >cd .
/home/l/g/tomotoboy/function >cd ..
/home/l/g/tomotoboy >findit sed.txt
./testdirec/sed.txt
./sed.txt
刪除shell函數
現在對函數做一些改動。首先刪除函數,使其對shell不可利用。使用unset命令完成此功能。刪除函數時unset命令格式為:
unset function_name
$unset findit
如果現在鍵入set命令,函數將不再顯示。
- /home/l/g/tomotoboy >unset findit
- /home/l/g/tomotoboy >set
- ……
- _=findit
- /home/l/g/tomotoboy >findit sed.txt
- -bash: findit: command not found
/home/l/g/tomotoboy >unset findit
/home/l/g/tomotoboy >set
……
_=findit
/home/l/g/tomotoboy >findit sed.txt
-bash: findit: command not found
再次定位函數
- /home/l/g/tomotoboy >. function/function.main
- /home/l/g/tomotoboy >findit sed.txt
- ./testdirec/sed.txt
- ./sed.txt
/home/l/g/tomotoboy >. function/function.main
/home/l/g/tomotoboy >findit sed.txt
./testdirec/sed.txt
./sed.txt
如果函數將從測試結果中反饋輸出,那么使用替換命令可保存結果。函數調用的替換格式為:
variable_name = variable_name
函數function_name輸出被設置到變量variable_name中。
- char_name(){
- # char_name
- # to call: char_name string
- # assign the argument across to new variable
- _LETTER_ONLY=$1
- # user awk to test for character only!
- _LETTER_ONLY=`echo $1|awk '{if ($0~/[^a-z A-Z]/) print 1}'`
- if [ "$_LETTER_ONLY" != "" ]
- then
- # oops errors
- return 1
- else
- # constains only chars
- return 0
- fi
- }
char_name(){
# char_name
# to call: char_name string
# assign the argument across to new variable
_LETTER_ONLY=$1
# user awk to test for character only!
_LETTER_ONLY=`echo $1|awk '{if ($0~/[^a-z A-Z]/) print 1}'`
if [ "$_LETTER_ONLY" != "" ]
then
# oops errors
return 1
else
# constains only chars
return 0
fi
}
- if char_name $F_NAME; then
- echo "OK"
- else
- echo "ERROR"
- fi
if char_name $F_NAME; then
echo "OK"
else
echo "ERROR"
fi
測試一下
- /home/l/g/tomotoboy/function >char_name hello
- /home/l/g/tomotoboy/function >echo $?
- 0
/home/l/g/tomotoboy/function >char_name hello
/home/l/g/tomotoboy/function >echo $?
0
注意^符號的使用,當直接用在第一個括號里,意指否定或不匹配括號里內容。[^a-z A-Z] 匹配任一非字母型字符,而[^0-9]匹配任一非數字型字符。