码迷,mamicode.com
首页 > 其他好文 > 详细

函数用法

时间:2015-09-04 18:40:51      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

函数用法

 

函数定义:

  1. # func_name 函数名    
  2. function func_name(){  
  3.     #函数体内容  
  4. }  
  5.   
  6. func_name(){  
  7.     #函数体内容  
  8. }  
函数调用:

    func_name parm

 

获取函数还回值:

  1. sum_xdr()
  2. {
  3. count=$1;
  4. count=$[count*10];
  5. echo $count;     #这里是使用echo语句,将结果输出到标准输出上,所以在主程序中可以使用变量接收
  6. }
  7. ret=`sum_xdr 3`;

 

 

 

1、函数调用(只能包含字符)


  1. #!/bin/sh
  2. char_name()
  3. {
  4. _Letters_Only=$1 #
  5. _Letters_Only=`echo $1|awk ‘{if($0~/[^a-zA-X]/) print "1"}‘` #只能包含字符
  6. if [ "$_Letters_Only" != "" ]
  7. then
  8. return 1
  9. else
  10. return 0
  11. fi
  12. }
  13. name_error()
  14. {
  15. echo "$@ contains errors ,it ,must contain only letters"
  16. }
  17. while :
  18. do
  19. echo -n "what is your first name :"
  20. read F_Name
  21. if char_name $F_Name
  22. then
  23. break
  24. else
  25. name_error $F_Name
  26. fi
  27. done

2、字符串转大写函数 str_to_upper 

  1. #!/bin/sh
  2. str_to_upper()
  3. {
  4.     _str=$1
  5.     if [ $# -ne 1 ]; then
  6.         echo "number_file: I need a string to convert please "
  7.         return 1
  8.     fi
  9.     echo $@ | tr ‘[a-z]‘ ‘[A-Z]‘
  10. }


3、列出文本文件的行号  number_file脚本

  1. #!/bin/sh
  2. number_file()
  3. {
  4. _Filename=$1
  5. if [ $# -ne 1 ]; then
  6. echo "number_file: I need a filename to number"
  7. return 1
  8. fi
  9. loop=1
  10. while read Line
  11. do
  12. echo "$loop :$Line"
  13. loop=`expr $loop + 1`
  14. done <$_Filename
  15. }

 

4、判断字符串是否为大写 is_upper脚本

  1. #!/bin/sh
  2. is_upper()
  3. {
  4. if [ $# -ne 1 ]; then
  5. echo "is_upper:I need a string to test OK"
  6. return 1
  7. fi
  8. _Is_Upper=`echo $1|awk ‘{if($0~/[^A-Z]/) print "1"}‘`
  9. if [ "$_Is_Upper" != "" ]
  10. then
  11. return 1
  12. else
  13. return 0
  14. fi
  15. }
  16. echo -n "Enter the filename :"
  17. read FileName
  18. if is_upper $FileName ; then
  19. echo "Great it‘s upper case"
  20. else
  21. echo "Sorry it‘s not upper case"
  22. fi

要测试字符串是否为小写,只需要在is_upper中替换相应的awk语句即可。此为

  1. is_lower
  2. _Is_Lower=`echo $1|awk ‘{if($0~/[^a-z]/) print "1"}‘`


5、 测试字符串长度 check_length脚本

  1. #!/bin/sh
  2. check_length()
  3. {
  4. _str=$1
  5. _max=$2
  6. if [ $# -ne 2 ]; then
  7. echo "check_length; I need a string and max length the string should be "
  8. return 1
  9. fi
  10. #check the length of the string
  11. _Length=`echo $_str|awk ‘{print length($0)}‘`
  12. if [ "$_Length" -gt "$_max" ]; then
  13. return 1
  14. else
  15. return 0
  16. fi
  17. }
  18. while :
  19. do
  20. echo "Enter your First name :"
  21. read Name
  22. if check_length $Name 10
  23. then
  24. break
  25. else
  26. echo "The name field is too long 10 charater max "
  27. fi
  28. done
使用wc命令接收键盘操作输入时有一个误操作。如果用户输入一个名字后,点击了几次空格键

wc也将这些空格作为字符串的一部分。因而给出其错误的长度。awk在读取键盘时缺省截去字符串末尾处空格。

 

6、chop函数删除字符串前面的字符 chop脚本

  1. #!/bin/sh
  2. chop()
  3. {
  4. # to call: chop string how_many_charts_to_chop
  5. _str=$1
  6. _chop=$2
  7. chop=`expr $_chop + 1 `
  8. if [ $# -ne 2 ]; then
  9. echo "check_length : I need a string and how manay characters to chop "
  10. return 1
  11. fi
  12. _Length=`echo $_str|awk ‘{print length($0)}‘`
  13. if [ "$_Length" -lt "$_chop" ]; then
  14. echo "sorry you have asked to chop more characters than there ara in the string "
  15. return 1
  16. fi
  17. echo $_str |awk ‘{print substr($1,‘$_chop‘)}‘
  18. }





函数用法

标签:

原文地址:http://www.cnblogs.com/liaohw/p/4782100.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!