博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ksh 函数
阅读量:4049 次
发布时间:2019-05-25

本文共 3293 字,大约阅读时间需要 10 分钟。

-- 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; 	fi		if [ $1 = $2 ]; then  	    return 0;	fi		return 2;}# 根据全局变量  max_value 得到函数的返回值function max {	if [[ $1 -gt $2 ]]; then      	max_value=$1;	else		max_value=$2;	fi}# 根据引用得到函数的返回值function max2 {	typeset -n r=$3; # 设置变量 r 引用 $3	if [[ $1 -gt $2 ]]; then      	r=$1;	else		r=$2;	fi}# 根据标准输出得到函数的返回值function min {	if [[ $1 -lt $2 ]]; then      	print $1;	else		print $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 value	hypot       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

-- 更多参见:

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

-- Last Updated on 2015-10-03

-- Written by ShangBo on 2015-09-24
-- End

你可能感兴趣的文章
周易、命理、风水、姓名与命运交流周易研究心得:姓名学
查看>>
解决asp.net中tabstrip不能点击的问题
查看>>
PB中使用blob进行文件读取的性能问题
查看>>
DataWindow.net中如何实现鼠标划过时变颜色
查看>>
Datawindow.net中设置字符串的显示,超过长度部分显示为。。。
查看>>
PowerBuilder中使用带返回的powerobjectparm
查看>>
从oracle表中随机取记录,产生随机数和随机字符串
查看>>
功夫熊猫,中国式的哲学和西方式的搞笑
查看>>
Oracle SYS口令深入解析
查看>>
XP中IIS“http500”错误的终极解决方法
查看>>
李开复眼中的兰迪教授:引领你的一生
查看>>
早起的虫儿被鸟吃?
查看>>
Love Your Life》—— 热爱生活
查看>>
一个高速交警的忠告
查看>>
新车装饰的中国特色
查看>>
没看过这么NB的自驾游,笑的我眼泪都出来了
查看>>
李涯的哭
查看>>
和机器学习和计算机视觉相关的数学
查看>>
论文MICO for MRI bias field estimation and tissue segmentation品讲
查看>>
后现代
查看>>