Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1110382
  • 博文数量: 300
  • 博客积分: 37
  • 博客等级: 民兵
  • 技术积分: 772
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-26 04:46
文章分类
文章存档

2017年(4)

2016年(7)

2015年(19)

2014年(72)

2013年(71)

2012年(127)

分类:

2012-09-15 23:58:09

原文地址:shell 函数小结 作者:qingshanli1988

函数定义
    shell允许将一组命令集或语句形成一个可用块,这些块称为shell函数
    定义函数的格式:
    function-name (){
        command1
        ........
    }
    或 function function-name(){ #函数名前面多了个function关键字
        command1
        ........
    }
2.函数调用
    以下是一个函数的脚本实例:
    #!/bin/bash
    #hello
    function hello(){       #声明函数
      echo "Hello!"     #函数的主体,输出"Hello!"
    }               #函数结束
    hello             #调用函数
3.参数传递
  向函数传递参数就像在脚本是使用变量位置$1,$2,$3...$9
  以下是一个传递参数的实例:
  #!/bin/bash
  #hellofun
  function hello(){
      echo "Hello! The first parameter is '$1'."
  }
  hello good
  #该脚本执行的结果是: Hello! The first parameter is 'good'.
4.函数文件
  保存函数的文件,用以上的例子写成一个函数文件如下:
  #!/bin/bash
  #hellofunction
  function hello(){
    echo "Hello!"
    return 1
  }
  上面的hellofunction文件就是一个函数文件,可通过另一个脚本来调用
  #!/bin/bash
  #hellof
  . hellofunction #注意点和hellofunction之间有个空格
  hello
5.载入和删除
  用set查看已载入的函数
  用unset function-name 取消载入
  举例如下:
#!/bin/bash
  #hellof
  . hellofunction
  unset
  hello   #因为已经取消载入。。所以会出错
6.函数返回状态
  #!/bin/bash
  #hellofun
  function hello(){
    echo "Hello! The first parameter is '$1'."
  }
  hello
  echo $?   #输出返回的状态值(一般成功是返回0,其它值为失败) 

 

 

你可以在bash shell的环境下编写函数,有两种定义函数的格式:
代码:
function functname()
{
shell commands
}
functname()
{
shell commands
}
下面是一个简单函数的脚本(ex1)示例:
代码:
message()
{
echo "message"
}
let i=1
while [ $i -le 3 ]
do
message
let i=$i+1
done
函数同样可以接受参数,$1存放第一个参数,$2存放第二个参数,$*存放输入参数的列表,...
代码:
$ more ex2
power()
{
x=$1
y=$2
count=1
result=1
while [ $count -le $y ]
do
result=`expr ${result} * $x`
count=`expr ${count} + 1`
done
echo $result
}
echo "Enter two numbers"
read num1 num2
echo -n "power is: "
power $num1 $num2
$ ex2
Enter two numbers
3 4
power is: 81
函数同样也可以返回值,使用return语句,在主程序(块)中,在调用函数之后保存返回状态$?的值.
代码:
power()
{
x=$1
y=$2
count=1
result=1
while [ $count -le $y ]
do
result=`expr ${result} * $x`
count=`expr ${count} + 1`
done
return $result
}
echo "Enter two numbers"
read num1 num2
power $num1 $num2
answer=$?
echo "$num1 to $num2 is $answer"
你也可以编写递归函数:
代码:
power()
{
x=$1
y=$2
if [ $y -eq 1 ]
then
return $x
else
y=`expr $y - 1`
power $x $y
result=`expr $? * $x`
return $result
fi
}
echo "Enter two numbers"
read num1 num2
power $num1 $num2
answer=$?
echo "$num1 to $num2 is $answer"
默认情况下,所有函数的变量都是全局变量.你可以用typeset去声明一个局部变量:
代码:
easy()
{
typeset a
a=`expr $1 + $2`
b=`expr $1 + $2`
echo "easy a is " $a
echo "easy b is " $b
}
a=10
b=20
easy $a $b
echo "global a is " $a
echo "global b is " $b
Output:
easy a is 30
easy b is 30
global a is 10
global b is 30
如果要在脚本中多次使用函数,可以把它放在一个函数目录中,像一个普通文件一样,使用的时候把它放在脚本开始的地方:
. functionfile
$ more power
代码:
power()
{
x=$1
y=$2
if [ $y -eq 1 ]
then
return $x
else
y=`expr $y - 1`
power $x $y
result=`expr $? * $x`
return $result
fi
}
$ more small
代码:
. power
echo "Enter two numbers:"
read x y
power $x $y
result=$?
echo "Answer is:" $result
$ small.ksh
2 5
Answer is: 32

阅读(526) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~