函数的使用
来源:互联网 发布:记录鼠标移动轨迹 js 编辑:程序博客网 时间:2024/05/18 05:32
两中格式用来定义函数:
fcuntion name {} 注意name和{之间有空格
name() {commands}
函数的使用:
#!/bin/bashfunction fun1 { //<span style="color:#FFFFFF;"><span style="background-color: rgb(255, 0, 0);">一定注意函数名和{之间的空格</span></span> echo "this is function"}count=1while [ $count -le 5 ]do fun1 count=$[ $count+1 ]done
注意:函数必须先定义在使用
返回值:
假设此时没有michael文件
#!/bin/bashfunction fun1 { echo "this is func1" ls -l mimchael echo "end fun1"}echo "testing the function"fun1echo "show the return value $?"
此时,可以知道状态为0,因为最后一条echo语句执行无误。如果没有最后一条echo,返回吗为2,因为没有michael文件,该条语句执行有误。
为了让自己可以掌控状态码,可以用return和函数输出
return:
#!/bin/bashfunction fun { read -p "input the value" value echo "double the value" return $[ $value*2 ]}funecho "return value is $?"~
需要说明一点。他这里的返回值表示的是状态吗,数值在0-255之间,超出就会报错。这里的返回值和Java、C是有区别的
函数输出:
为了解决0-255之间的问题,使用
函数输出。
#!/bin/bashfunction fun { read -p "input the value" value echo $[ $value*2 ] <span style="color:#FF0000;">//注意这是echo,不是return</span>}result=`fun`echo "return value is $result"
函数中使用变量:
具体用法和shell中的用法相同。
#!/bin/bashfunction fun { if [ $# -eq 0 ] || [ $# -gt 2 ] then echo "para is too much or little" elif [ $# -eq 1 ] then echo $[ $1+$1 ] elif [ $# -eq 2 ] then echo $[ $1+$2 ] fi}echo -n "Adding 10 and 15 " value=`fun 10 15`echo $valueecho -n "DOUBLE 10 "value=`fun 10`echo $valueecho -n "no para"value=`fun`echo $valueecho -n "too much para"value=`fun 1 2 3`echo $value
变量的作用域:
全局变量:默认情况下,在shell脚本中任何地方申明的值都是全局变量,包括在函数内申明的值。
局部变量:在函数内部申明变量时,加上local关键字即可。
#!/bin/bashfunction fun1 { local temp=$[ $value+5 ] result=$[ $temp*2 ]}temp=4value=6fun1echo "the result is $result"if [ $temp -gt $valule ]then echo "temp is large"else echo "temp is small"fi
数组变量和函数:
如果需要将一个数组的数据传入到函数之中:需要如下操作:
#!/bin/bashfunction testit { echo "there is $# params" thisarray=(`echo "$@"`) //<span style="color:#FF0000;">从命令行参数重建该数组变量</span> echo "the receive array is ${thisarray[*]}"}myarray=(1 2 3 4 5)echo "the original array is:${myarray[*]}"testit ${myarray[*]}~如果需要返回一个数组呢?
function fun {local oriArraylocal newArraylocal length=$[ $#-1 ]oriArray=(`echo "$@"`)for(( i=0;i<=length;i++)){newArray[$i]=$[ ${oriArray[$i]}*2 ]}echo ${newArray[*]}}oriArray=(1 2 3 4 5)echo "ori Array is ${oriArray[*]}"arg1=(`fun ${oriArray[*]}`) //主要是接受的形式:<span style="color:#FF0000;"> (`函数名 参数`)</span>echo "the new array is ${arg1[*]}"
创建库
如果在一个脚本中定义了一个函数,如果说次函数需要被多个其他脚本使用。应该怎么做呢?
需要在新的脚本中引用一下函数脚本:
. ./函数脚本明 //注意之间有空格
0 0
- 使用excel的函数
- format函数的使用
- format函数的使用
- SetTimer函数的使用
- showModalDialog函数的使用
- 函数对象的使用
- DECODE函数的使用
- IIF函数的使用
- AnimateWindow()函数的使用
- count(*)函数的使用
- ceiling函数的使用
- strcat函数的使用
- 常用函数的使用
- translate函数的使用
- va函数的使用
- 函数对象的使用
- sprintf函数的使用
- WaitForSingleObject函数的使用
- STL之迭代器
- bundle contains data files不应该被直接修改
- Leetcode31: Contains Duplicate
- Android Activity生命周期都该做哪些事情?
- 基于.Net的单点登录(SSO)解决方案(简易方案以及后续更新成熟方案)
- 函数的使用
- VC:正在驱动科技与人类生活的十大趋势
- 15第十二周项目三——日期时间类
- centos下部署ngnix和php
- Linux学习笔记(05-25)进程优先级设置和进程调度
- HADOOP入门-单机模式安装HADOOP
- 开发者必知的KPI概念和用法
- 穿透Session 0 隔离(一)
- 漫谈 Clustering (2): k-medoids