Chinaunix首页 | 论坛 | 博客
  • 博客访问: 659029
  • 博文数量: 125
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 962
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-14 10:20
个人简介

我是一只小白兔~

文章分类

全部博文(125)

文章存档

2017年(16)

2016年(108)

2014年(1)

我的朋友

分类: LINUX

2016-11-10 14:22:06

这篇文章主要介绍了Shell函数的7种用法介绍,本文讲解了在shell文件内部定义函数并引用、返回值、函数输出、向函数传递参数、全局变量与局部变量等内容,需要的朋友可以参考下。

1. 在shell文件内部定义函数并引用:

点击(此处)折叠或打开

  1. [~/shell/function]# cat factorial.sh
  2. #!/bin/bash
  3. function factorial
  4. {
  5. factorial=1
  6. for (( i=1;i <= $1;i++ ))
  7.         do
  8.         factorial=$[ $factorial * $i ]
  9.         done
  10. echo $1的阶乘是:$factorial
  11. }
  12. echo '程序名':$0,用于求阶乘
  13. factorial $1
  14. [~/shell/function]# ./factorial.sh 10
程序名:./factorial.sh,用于求阶乘
10的阶乘是:3628800

2.返回值

函数返回码是指函数最后一条命令的状态码,可以用于函数返回值
使用return命令手动指定返回值:


点击(此处)折叠或打开

  1. [~/shell/function]# cat return.sh
  2. #!/bin/bash
  3. function fun1 {
  4.   read -p "enter a: " a
  5.   echo -n "print 2a: "
  6.   return $[ $a * 2 ]
  7. }
  8. fun1
  9. echo "return value $?"
  10. [~/shell/function]# ./return.sh
  11. enter a: 100
  12. print 2a: return value 200
由于shell状态码最大是255,所以当返回值大于255时会出错。

点击(此处)折叠或打开

  1. [~/shell/function]# ./return.sh
  2. enter a: 200
  3. print 2a: return value 144

3.函数输出

为了返回大于255的数、浮点数和字符串值,最好用函数输出到变量:


点击(此处)折叠或打开

  1. [~/shell/function]# cat ./fun_out.sh
  2. #!/bin/bash
  3. function fun2 {
  4.   read -p "enter a: " a             #接收用户输入
  5.   echo -n "print 2a: "
  6.   echo $[ $a * 2 ]
  7. }
  8. result=`fun2`                          #这里是反单引号
  9. echo "return value $result"
  10. [~/shell/function]# ./fun_out.sh
  11. enter a: 400
  12. return value print 2a: 800
4.向函数传递参数(使用位置参数):


点击(此处)折叠或打开

  1. [~/shell/function]# cat ./parameter.sh
  2. #!/bin/bash
  3. if [ $# -ne 3 ]
  4. then
  5.     echo "usage: $0 a b c"
  6.     exit
  7. fi
  8. fun3() {
  9.     echo $[ $1 * $2 * $3 ]
  10. }
  11. result=`fun3 $1 $2 $3`
  12. echo the result is $result
  13. [~/shell/function]# ./parameter.sh 1 2 3
  14. the result is 6
  15. [~/shell/function]# ./parameter.sh 1 2
  16. usage: ./parameter.sh a b c

5.全局变量与局部变量

默认条件下,在函数和shell主体中建立的变量都是全局变量,可以相互引用,当shell主体部分与函数部分拥有名字相同的变量时,可能会相互影响,例如:


点击(此处)折叠或打开

  1. [~/shell/function]# cat ./variable.sh
  2. #!/bin/bash
  3. if [ $# -ne 3 ]
  4. then
  5.     echo "usage: $0 a b c"
  6.     exit
  7. fi
  8. temp=5
  9. value=6
  10. echo temp is: $temp
  11. echo value is: $value
  12. fun3() {
  13.     temp=`echo "scale=3;$1*$2*$3" | bc -ql`
  14.     result=$temp
  15. }
  16. fun3 $1 $2 $3
  17. echo "the result is $result"
  18. if [ `echo "$temp > $value" | bc -ql` -ne 0 ]
  19. then
  20.     echo "temp is larger"
  21. else
  22.     echo "temp is still smaller"
  23. fi
  24. [~/shell/function]# ./variable.sh 12 3 2
  25. temp is: 5
  26. value is: 6
  27. the result is 72
  28. temp is larger
在这种情况下,在函数内部最好使用局部变量,消除影响。

点击(此处)折叠或打开

  1. [~/shell/function]# cat ./variable.sh
  2. #!/bin/bash
  3. if [ $# -ne 3 ]
  4. then
  5.     echo "usage: $0 a b c"
  6.     exit
  7. fi
  8. temp=5
  9. value=6
  10. echo temp is: $temp
  11. echo value is: $value
  12. fun3() {
  13.     local temp=`echo "scale=3;$1*$2*$3" | bc -ql`            #多了个local关键字
  14.     result=$temp
  15. }
  16. fun3 $1 $2 $3
  17. echo "the result is $result"
  18. if [ `echo "$temp > $value" | bc -ql` -ne 0 ]
  19. then
  20.     echo "temp is larger"
  21. else
  22.     echo "temp is still smaller"
  23. fi
  24. [~/shell/function]# ./variable.sh 12 3 2
  25. temp is: 5
  26. value is: 6
  27. the result is 72
  28. temp is still smaller
6.向函数传递数组变量:


点击(此处)折叠或打开

  1. [~/shell/function]# cat array.sh
  2. #!/bin/bash
  3. a=(11 12 13 14 15)
  4. echo ${a[*]}                                                            #打印数组的所有元素
  5. function array(){
  6.   echo parameters : "$@"                     #打印参数列表
  7.   local factorial=1
  8.   for value in "$@"
  9.   do
  10.     factorial=$[ $factorial * $value ]
  11.   done
  12.   echo $factorial
  13. }
  14. array ${a[*]}
  15. [~/shell/function]# ./array.sh
  16. 11 12 13 14 15
  17. parameters : 11 12 13 14 15
  18. 360360
7.函数返回数组变量

点击(此处)折叠或打开

  1. [~/shell/function]# cat array1.sh
  2. #!/bin/bash
  3. a=(11 12 13 14 15)
  4. function array(){
  5.   echo parameters : "$@"
  6.   local newarray=(`echo "$@"`)
  7.   local element="$#"                                         #参数的个数
  8.   local i
  9.   for (( i = 0; i < $element; i++ ))
  10.   {
  11.     newarray[$i]=$[ ${newarray[$i]} * 2 ]
  12.   }
  13.   echo new value:${newarray[*]}
  14. }
  15. result=`array ${a[*]}`
  16. echo ${result[*]}
  17. [~/shell/function]# ./array1.sh
  18. parameters : 11 12 13 14 15 new value:22 24 26 28 30
阅读(1654) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~