函數(shù)由兩部分組成:
- 函數(shù)標(biāo)題。
- 函數(shù)體。
標(biāo)題是函數(shù)名。函數(shù)體是函數(shù)內(nèi)的命令集合。標(biāo)題名應(yīng)該唯一;如果不是,將會(huì)混淆結(jié),因?yàn)槟_本在查看調(diào)用腳本前將首先搜索函數(shù)調(diào)用相應(yīng)的 s h e l l。
定義函數(shù)的格式為:
- 函數(shù)名()
- {
- 命令1
- . . .
- }
- 或者
- function 函數(shù)名()
- { ...
- }
函數(shù)名()
{
命令1
. . .
}
或者
function 函數(shù)名()
{ ...
}
兩者方式都可行。如果愿意,可在函數(shù)名前加上關(guān)鍵字function,這取決于使用者。
創(chuàng)建函數(shù)文件
下面創(chuàng)建包容函數(shù)的函數(shù)文件并將之載入shell,進(jìn)行測(cè)試,再做改動(dòng),之后再重新載入。
函數(shù)文件名為functions.main,內(nèi)容如下
- #!/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
現(xiàn)在文件已經(jīng)創(chuàng)建好了,要將之載入shell,試鍵入:
$. functions.main
如果返回信息file not found,再試:
$. /functions.main
此即<點(diǎn)> <空格> <斜線> <文件名>,現(xiàn)在文件應(yīng)該已載入shell。如果仍有錯(cuò)誤,則應(yīng)該仔細(xì)檢查是否鍵入了完整路徑名
檢查載入函數(shù)
使用set命令確保函數(shù)已載入。set命令將在shell中顯示所有的載入函數(shù)。
- /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
}
執(zhí)行shell函數(shù)
要執(zhí)行函數(shù),簡(jiǎn)單地鍵入函數(shù)名即可。這里是帶有一個(gè)參數(shù)的 findit函數(shù),參數(shù)是某個(gè)文件
- /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函數(shù)
現(xiàn)在對(duì)函數(shù)做一些改動(dòng)。首先刪除函數(shù),使其對(duì)shell不可利用。使用unset命令完成此功能。刪除函數(shù)時(shí)unset命令格式為:
unset function_name
$unset findit
如果現(xiàn)在鍵入set命令,函數(shù)將不再顯示。
- /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
再次定位函數(shù)
- /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
如果函數(shù)將從測(cè)試結(jié)果中反饋輸出,那么使用替換命令可保存結(jié)果。函數(shù)調(diào)用的替換格式為:
variable_name = variable_name
函數(shù)function_name輸出被設(shè)置到變量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
測(cè)試一下
- /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
注意^符號(hào)的使用,當(dāng)直接用在第一個(gè)括號(hào)里,意指否定或不匹配括號(hào)里內(nèi)容。[^a-z A-Z] 匹配任一非字母型字符,而[^0-9]匹配任一非數(shù)字型字符。