1. 在shell文件内部定义函数并引用:
[~/shell/function]# cat factorial.sh
#!/bin/bash
function factorial
{
factorial=1
for (( i=1;i <= $1;i++ ))
do
factorial=$[ $factorial * $i ]
done
echo $1的阶乘是:$factorial
}
echo '程序名':$0,用于求阶乘
factorial $1
[~/shell/function]# ./factorial.sh 10
程序名:./factorial.sh,用于求阶乘
10的阶乘是:3628800
2.返回值
函数返回码是指函数最后一条命令的状态码,可以用于函数返回值
使用return命令手动指定返回值:
[~/shell/function]# cat return.sh
#!/bin/bash
function fun1 {
read -p "enter a: " a
echo -n "print 2a: "
return $[ $a * 2 ]
}
fun1
echo "return value $?"
[~/shell/function]# ./return.sh
enter a: 100
print 2a: return value 200
由于shell状态码最大是255,所以当返回值大于255时会出错
[~/shell/function]# ./return.sh
enter a: 200
print 2a: return value 144
3.函数输出
为了返回大于255的数、浮点数和字符串值,最好用函数输出到变量:
[~/shell/function]# cat ./fun_out.sh
#!/bin/bash
function fun2 {
read -p "enter a: " a
echo -n "print 2a: "
echo $[ $a * 2 ]
}
result=`fun2`
echo "return value $result"
[~/shell/function]# ./fun_out.sh
enter a: 400
return value print 2a: 800
4.向函数传递参数(使用位置参数):
[~/shell/function]# cat ./parameter.sh
#!/bin/bash
if [ $# -ne 3 ]
then
echo "usage: $0 a b c"
exit
fi
fun3() {
echo $[ $1 * $2 * $3 ]
}
result=`fun3 $1 $2 $3`
echo the result is $result
[~/shell/function]# ./parameter.sh 1 2 3
the result is 6
[~/shell/function]# ./parameter.sh 1 2
usage: ./parameter.sh a b c
5.全局变量与局部变量
默认条件下,在函数和shell主体中建立的变量都是全局变量,可以相互引用,当shell主体部分与函数部分拥有名字相同的变量时,可能会相互影响,例如:
[~/shell/function]# cat ./variable.sh
#!/bin/bash
if [ $# -ne 3 ]
then
echo "usage: $0 a b c"
exit
fi
temp=5
value=6
echo temp is: $temp
echo value is: $value
fun3() {
temp=`echo "scale=3;$1*$2*$3" | bc -ql`
result=$temp
}
fun3 $1 $2 $3
echo "the result is $result"
if [ `echo "$temp > $value" | bc -ql` -ne 0 ]
then
echo "temp is larger"
else
echo "temp is still smaller"
fi
[~/shell/function]# ./variable.sh 12 3 2
temp is: 5
value is: 6
the result is 72
temp is larger
在这种情况下,在函数内部最好使用局部变量,消除影响
[~/shell/function]# cat ./variable.sh
#!/bin/bash
if [ $# -ne 3 ]
then
echo "usage: $0 a b c"
exit
fi
temp=5
value=6
echo temp is: $temp
echo value is: $value
fun3() {
local temp=`echo "scale=3;$1*$2*$3" | bc -ql`
result=$temp
}
fun3 $1 $2 $3
echo "the result is $result"
if [ `echo "$temp > $value" | bc -ql` -ne 0 ]
then
echo "temp is larger"
else
echo "temp is still smaller"
fi
[~/shell/function]# ./variable.sh 12 3 2
temp is: 5
value is: 6
the result is 72
temp is still smaller
6.向函数传递数组变量:
[~/shell/function]# cat array.sh
#!/bin/bash
a=(11 12 13 14 15)
echo ${a[*]}
function array(){
echo parameters : "$@"
local factorial=1
for value in "$@"
do
factorial=$[ $factorial * $value ]
done
echo $factorial
}
array ${a[*]}
[~/shell/function]# ./array.sh
11 12 13 14 15
parameters : 11 12 13 14 15
360360
7.函数返回数组变量
[~/shell/function]# cat array1.sh
#!/bin/bash
a=(11 12 13 14 15)
function array(){
echo parameters : "$@"
local newarray=(`echo "$@"`)
local element="$#"
local i
for (( i = 0; i < $element; i++ ))
{
newarray[$i]=$[ ${newarray[$i]} * 2 ]
}
echo new value:${newarray[*]}
}
result=`array ${a[*]}`
echo ${result[*]}
[~/shell/function]# ./array1.sh
parameters : 11 12 13 14 15 new value:22 24 26 28 30
相关推荐
Linux函数大全,包含常用函数说明,方便日常编程查询使用。
Linux函数大全.rar,用的时候查查挺好哈哈
linux函数手册.rar
易语言源码易语言调用linux函数源码.rar 易语言源码易语言调用linux函数源码.rar 易语言源码易语言调用linux函数源码.rar 易语言源码易语言调用linux函数源码.rar 易语言源码易语言调用linux函数源码.rar ...
在具有一定规模的代码中(C 语言),调用第三方动态库中的函数来完成一些功能,是很常见的工作场景。 假设现在有一项任务:需要在调用某个动态库中的某个函数的之前和之后,做一些额外的处理工作。 这样的需求一般称作...
记录linux系统中绝大部分函数讲解以及使用的例子。
Linux函数大全
这是一个很全面的中文的函数LINUX库文件。
linux 函数linux 函数linux 函数linux 函数linux 函数linux 函数linux 函数linux 函数linux 函数
易语言调用linux函数源码,调用linux函数,load_so,get_function_addr,system,printf,abc
Linux C函数库参考手册是一套Linux C函数使用手册,这里还提供了LinuxC函数手册chm,帮助大家查找。在Linux下,使用到的C语言函数中文手册,全都有实例,如果你是大神,完全不需要,如果你对英文的手册感到头疼,...
#ifndef __TIME_TRACE_UTILS__ #define __TIME_TRACE_UTILS__ #include #include #include #include #include #include #include #include ...#define NS_TT_START namespace tt { ...# define TIME_TRACE tt::...
堆栈是一种先进后出的数据结构,是计算机中最常用的一种数据结构,堆栈可以用数组 实现,也可以用链表实现。其中,堆(heap)中存放的是用malloc 分配地址的动态变量或 c++中的对象等数据;栈(stack )又称称为堆栈...
让你对开发中的linux函数有个亲密接触。
讲述了linux中常用的函数库,是linux编程必须用到的。
c++钩子函数:copy hook c++调用钩子函数监视复制文件操作
一个十分祥细的Linux函数参考手册,有着十分祥细的函数使用例子
Linux函数手册等.zip