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
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 《蛋仔派对》发条鹦鹉巴洛皮肤图鉴
- 沈文程1988-畅销曲精华[爱莉亚][WAV+CUE]
- Brahms-ViolinConcerto,C.Schumann-3Romances-Batiashvili(2013)[96-24]
- Tchaikovsky-SymphonyNo.6-Nezet-Seguin,LisaBatiashvili(2014)[96-24].
- 《鸦卫奇旅》自定义是不是解锁不了难度
- 《海贼王赏金猎人》配置要求一览
- 《狗钩宇宙狗狗》联机失败解决方法
- 两款《宝可梦》国行版发售!售价299元 还有新外设
- 《塞尔达》新作常规通关20小时 全收集需30小时
- 腾讯Switch开启假日折扣!多款国产游戏迎史低
- 群星.1998-在那一年2CD【宝丽金】【WAV+CUE】
- 黄征.2012-征爱·拾力【乐华娱乐】【FLAC分轨】
- 凤飞飞.1998-凤飞飞30周年飞精选CD书2CD【歌林】【WAV+CUE】
- 《发烧大牌榜 精雕细琢的珍品 10CD》[WAV/分轨][5.1GB]
- 《张学友 最经典发烧金典》[WAV/分轨][600MB]