黑松山资源网 Design By www.paidiu.com

一、定义shell函数(define function)

语法:

[ function ] funname [()]
{
action;
[return int;]
}

说明:

1、可以带function fun() 定义,也可以直接fun() 定义,不带任何参数。
2、参数返回,可以显示加:return 返回,如果不加,将以最后一条命令运行结果,作为返回值。 return后跟数值n(0-255

实例(testfun1.sh):

#!/bin/sh 
  
 fSum 3 2; 
 function fSum() 
 { 
   echo $1,$2; 
   return $(($1+$2)); 
 } 
 fSum 5 7; 
 total=$(fSum 3 2); 
 echo $total,$"htmlcode">
#!/bin/sh 
 
echo $(uname); 
declare num=1000; 
 
uname() 
{ 
  echo "test!"; 
  ((num++)); 
  return 100; 
} 
testvar() 
{ 
  local num=10; 
  ((num++)); 
  echo $num; 
 
} 
 
uname; 
echo $"color: #ff0000">以下是其它网友的补充:

本教程目前为止所有脚本都是从头到尾执行。这样做很好,但你也许已经注意到有些脚本段间互相重复。

shell允许将一组命令集或语句形成一个可用块,这些块称为shell函数。

shell中函数的定义格式如下:

函数名(){
command1
command2
...
commandN
[ return value ]
}

如果愿意,可在函数名前加上关键字function,这取决于使用者。
function 函数名(){
command1
command2
...
commandN
[ return value ]
}

函数返回值,可以显示增加return语句;如果不加,则将最后一条命令运行结果作为返回值(一般为0,如果执行失败则返回错误代码)。 return后跟数值(0-255)。

函数可以放在同一个文件中作为一段代码,也可以放在只包含函数的单独文件中。函数不必包含很多语句或命令,甚至可以只包含一个echo语句,这取决于使用者。

下面的例子定义了一个函数并进行调用:

#!/bin/bash
demoFun(){
echo "This is your first shell function!"
}
echo "Function begin..."
hello
echo "Function end!"输出:
Function begin...
This is your first shell function!
Function end!

下面定义一个带有return语句的函数:

#!/bin/bash
funWithReturn(){
echo "The function is to get the sum of two numbers..."
echo -n "Input first number: "
read aNum
echo -n "Input another number: "
read anotherNum
echo "The two numbers are $aNum and $anotherNum !"
return $(($aNum+$anotherNum))
}
funWithReturn
echo "The sum of two numbers is $"输出类似下面:
The function is to get the sum of two numbers...
Input first number: 25
Input another number: 50
The two numbers are 25 and 50 !
The sum of two numbers is 75 !

函数返回值在调用该函数后通过 $? 来获得。

注意:所有函数在使用前必须定义。这意味着必须将函数放在脚本开始部分,直至shell解释器首次发现它时,才可以使用。调用函数仅使用其函数名即可。

黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。