ksh 函数

来源:互联网 发布:禁止修改ip软件 编辑:程序博客网 时间:2024/06/07 06:58

-- Start

#!/bin/ksh  ################### 函数必须先定义后使用# 定义函数方式 1: ksh 语法function fun_test1 {print "This is function fun_test1.";}# 定义函数方式 2: POSIX 语法fun_test2 (){print "This is function fun_test2.";}# 参数function fun_test3 {print "Enter function $0"; # $0 表示函数名print "The first parameter is $1"; # $1 引用第一个参数print "The second parameter is $2"; # $2 引用第二个参数print "This function has $# parameters"; # $# 表示参数的个数print "You are calling $0 $*"; # $* 是个数组,存储所有的参数print "You are calling $0 $@"; # $@ 是个数组,存储所有的参数}# return 语句返回代码给调用者# 0 代表函数执行成功, 其他值代表失败function equal {if [[ -z $1 || -z $2 ]];then      return 1; fiif [ $1 = $2 ]; then      return 0;fireturn 2;}# 根据全局变量  max_value 得到函数的返回值function max {if [[ $1 -gt $2 ]]; then      max_value=$1;elsemax_value=$2;fi}# 根据引用得到函数的返回值function max2 {typeset -n r=$3; # 设置变量 r 引用 $3if [[ $1 -gt $2 ]]; then      r=$1;elser=$2;fi}# 根据标准输出得到函数的返回值function min {if [[ $1 -lt $2 ]]; then      print $1;elseprint $2;fi}################### Main# 调用函数fun_test1;fun_test2;fun_test3 a b c d;equal;print "Call equal function end with error code $?"; # $? 表示上个命令返回的代码typeset -i max_value=0;max 1 2;print "the bigger value of 1 and 2 is $max_value";typeset -i max_value=0;max2 3 4 max_value;print "the bigger value of 3 and 4 is $max_value";# `` 用来捕获函数或命令的标准输入到变量中smaller=`min 1 2`;print "the smaller value of 1 and 2 is $smaller";# $() 用来捕获函数或命令的标准输入到变量中smaller=$(min 3 4);print "the smaller value of 3 and 4 is $smaller";# 删除函数unset -f fun_test1;unset -f fun_test2;

注意,我们可以从 $* 或 $@ 数组中得到函数的所有参数,但是它们有非常大的区别。

#!/bin/ksh################### 函数function fun_test {print 'iterating array $*';  typeset -i i=1;for p in $*;  do      print "The ${i}th parameter is [$p]";      ((i++));  done;print 'iterating array "$*"';i=1;for p in "$*";  do      print "The ${i}th parameter is [$p]";      ((i++));  done;print 'iterating array $@';i=1;  for p in $@;  do      print "The ${i}th parameter is [$p]";      ((i++));  done;print 'iterating array "$@"';i=1;for p in "$@";  do      print "The ${i}th parameter is [$p]";      ((i++));  done;}################### Mainfun_test abc "x yz" "1 2 \n3";

结果如下:

iterating array $*The 1th parameter is [abc]The 2th parameter is [x]The 3th parameter is [yz]The 4th parameter is [1]The 5th parameter is [2]The 6th parameter is [3]iterating array "$*"The 1th parameter is [abc x yz 1 2 3]iterating array $@The 1th parameter is [abc]The 2th parameter is [x]The 3th parameter is [yz]The 4th parameter is [1]The 5th parameter is [2]The 6th parameter is [3]iterating array "$@"The 1th parameter is [abc]The 2th parameter is [x yz]The 3th parameter is [1 2 3]

从上面的结果中可以看出,只有 "$@" 能保证正确,注意,有双引号的哦。


内置数学函数

函数        返回值abs        Absolute valuehypot       Euclidean distanceacos    Arc cosineint         Integer partsin        Arc sinelog        Natural logarithmatan        Arc tangentpow         Exponentiation (xy)atan2       Arc tangent of two variablessin        Sinecos        Cosinesinh        Hyperbolic sinecosh    Hyperbolic cosinesqrt        Square rootexp         Exponential (ex)tan        Tangentfmod        Floating-point remaindertanh        Hyperbolic tangent


-- 更多参见:ksh 精萃

-- 声 明:转载请注明出处

-- Last Updated on 2015-10-03
-- Written by ShangBo on 2015-09-24
-- End


0 0
原创粉丝点击