无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

shell函数

Posted on 2009-11-29 12:09 Gavin.lee 阅读(372) 评论(0)  编辑  收藏 所属分类: Linux shell 入门

 

函数由两部分组成:
  • 函数标题。
  • 函数体。

标题是函数名。函数体是函数内的命令集合。标题名应该唯一;如果不是,将会混淆结,因为脚本在查看调用脚本前将首先搜索函数调用相应的 s h e l l。
定义函数的格式为:
Shell代码
  1. 函数名()   
  2. {    
  3. 命令1  
  4. . . .   
  5. }   
  6. 或者   
  7. function 函数名()   
  8. {   ...   
  9. }  


两者方式都可行。如果愿意,可在函数名前加上关键字function,这取决于使用者。

创建函数文件
下面创建包容函数的函数文件并将之载入shell,进行测试,再做改动,之后再重新载入。
函数文件名为functions.main,内容如下
Shell代码
  1. #!/bin/sh   
  2. #functions.main   
  3. #   
  4. #findit: this is front end for the basic find command   
  5. findit() {   
  6. #findit   
  7. if [ $# -lt 1 ]; then   
  8. echo "usage : findit file"  
  9. return 1;   
  10. fi   
  11. find . -name $1 -print   
  12. }  
定位文件
定位文件格式为:
. /pahname/filename
现在文件已经创建好了,要将之载入shell,试键入:
$. functions.main
如果返回信息file not found,再试:
$. /functions.main
此即<点> <空格> <斜线> <文件名>,现在文件应该已载入shell。如果仍有错误,则应该仔细检查是否键入了完整路径名

检查载入函数
使用set命令确保函数已载入。set命令将在shell中显示所有的载入函数。
Shell代码
  1. /home/l/g/tomotoboy/function >. function.main   
  2. /home/l/g/tomotoboy/function >set   
  3. ……   
  4. _=function.main   
  5. findit ()   
  6. {   
  7.     if [ $# -lt 1 ]; then   
  8.         echo "usage : findit file";   
  9.         return 1;   
  10.     fi;   
  11.     find . -name $1 -print   
  12. }  
执行shell函数
要执行函数,简单地键入函数名即可。这里是带有一个参数的 findit函数,参数是某个文件
Shell代码
  1. /home/l/g/tomotoboy/function >cd .   
  2. /home/l/g/tomotoboy/function >cd ..   
  3. /home/l/g/tomotoboy >findit sed.txt   
  4. ./testdirec/sed.txt   
  5. ./sed.txt  


删除shell函数
现在对函数做一些改动。首先删除函数,使其对shell不可利用。使用unset命令完成此功能。删除函数时unset命令格式为:
unset  function_name
$unset findit
如果现在键入set命令,函数将不再显示。
Shell代码
  1. /home/l/g/tomotoboy >unset findit   
  2. /home/l/g/tomotoboy >set   
  3. ……   
  4. _=findit   
  5. /home/l/g/tomotoboy >findit sed.txt   
  6. -bash: findit: command not found  


再次定位函数
Shell代码
  1. /home/l/g/tomotoboy >. function/function.main   
  2. /home/l/g/tomotoboy >findit sed.txt   
  3. ./testdirec/sed.txt   
  4. ./sed.txt  

如果函数将从测试结果中反馈输出,那么使用替换命令可保存结果。函数调用的替换格式为:
variable_name = variable_name
函数function_name输出被设置到变量variable_name中。
Shell代码
  1. char_name(){   
  2. # char_name   
  3. # to call: char_name string   
  4. # assign the argument across to new variable   
  5. _LETTER_ONLY=$1  
  6. # user awk to test for character only!   
  7. _LETTER_ONLY=`echo $1|awk '{if ($0~/[^a-z A-Z]/) print 1}'`   
  8. if [ "$_LETTER_ONLY" != "" ]   
  9. then   
  10.     # oops  errors   
  11.     return 1  
  12. else   
  13.     # constains only chars   
  14.     return 0  
  15. fi   
  16. }  

Shell代码
  1. if char_name $F_NAME; then   
  2.  echo "OK"  
  3. else   
  4.   echo "ERROR"  
  5. fi  
测试一下
Shell代码
  1. /home/l/g/tomotoboy/function >char_name hello   
  2. /home/l/g/tomotoboy/function >echo $?   
  3. 0  
注意^符号的使用,当直接用在第一个括号里,意指否定或不匹配括号里内容。[^a-z A-Z] 匹配任一非字母型字符,而[^0-9]匹配任一非数字型字符。

只有注册用户登录后才能发表评论。


网站导航: