博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux脚本编程-函数创建
阅读量:7097 次
发布时间:2019-06-28

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

hot3.png

1.函数的基本格式

function name {   commands}//或者name(){}

2.使用函数

#!/bin/bash#testing functionfunction func1 {  echo "This is a example if function"}count=1while [ $count -lt 10 ]do echo $count func1 count=$[ $count+1 ]done

函数退出状态码

#!/bin/bash#testing function exit statusfunc1() {  echo "Try to diplay a no exitent file"}func1echo $?

函数return

#!/bin/bash#using the return command in a functionfunction db1() {  return $[ 2*5 ]}a=`db1`echo $a

3.向函数传递参数

#!/bin/bash#passing parameters to a functionfunction addem {  if [ $# -eq 0 ] || [ $# -gt 2]  then     echo -1  elif [ $# -eq 1 ]  then     echo $[ $1+$2 ]  else     echo $[ $1+$2 ]  fi}echo -n "Adding 10 and 15:"value=`addem 10 15`echo $valueecho -n "Let's try adding just one numbers:"value=`addem 10`echo -n "Now trying adding no numbers:"value=`addem`echo $valueecho -n "Finaly, try adding three numbers:"value=`addem 10 15 20`

没有参数是或者多余要求的参数,函数都会返回-1,如果只有一个参数,addem会将自己加到自己上面生成结果

全局变量:

#!/bin/bash#using a global variable to pass a valuefunction db1 {  value=$[ $value*2 ]; }read -p "Enter a value:" valuedb1echo "The new value is:$value"

数组变量和函数

#!/bin/bash#trying to pass an array variablefunction testit {  echo "The parameters are:$@"  thisarray=$1  echo "The recevied array is ${thisarray[*]}" }myarray=(1 2 3 4 5)echo "The orginial array is :${myarray[*]}"testit $myarray

函数库创建

#my script functionsfunction addem {  echo $[ $1+$2 ]}function multem {  echo $[ $1*$2 ]}function divem { if [ $2 -ne 0 ] then   echo $[ S1/$2 ] else   echo -1 fi}

库的引用(库引用在前面加上两个点好)

#!/bin/bash#using functions defined in a library file../myfuncsvalue1=10value2=5result1=`addem $value $value2`result2=`multem $value1 $value2`result3=`divem $value1 $value2`echo "The result of adding them is:$result1"echo "The result of multiplying them is:$result2"echo "The result of dividing them is:$result3"

递归获取文件

function getdir(){    echo $1    for file in $1/*    do    if test -f $file    then        echo $file        arr=(${arr[*]} $file)    else        getdir $file    fi    done}getdir /usr/local/recommend/config/echo "INFO **************************"echo ${arr[@]}bar=$(IFS=, ; echo "${arr[*]}")echo $bar

获取目录下的文件

for f in `ls`do  if [[ $f == *.yaml ]]  then     //do something  fidone

shell数组操作(数组切片)

#!/bin/bash arr=(192.168.237.129 192.168.237.130 172.168.9.1 192.168.237.131) a=3 echo ${arr[@]:0:a} new_arr=(${arr[@]:0:a}) for ip in ${new_arr[*]} do   echo $ip done

shell读取文件到数组

#!/bin/bashCUR_PATH=$(cd `dirname $0`;pwd)echo "INFO: current excute path is ${CUR_PATH}"file=${CUR_PATH}/node.conflines=$(cat $file)for line in $lines; do        echo "$line"        arr=(${arr[*]} $line)doneecho ${arr[@]}

转载于:https://my.oschina.net/u/1760791/blog/714028

你可能感兴趣的文章
mysql 表导出/导入
查看>>
Java垃圾回收简介
查看>>
家用无线路由器的设置
查看>>
我的友情链接
查看>>
nginx location匹配规则
查看>>
pip、easy_install切换国内源
查看>>
2000字毕业个人自我鉴定范文
查看>>
eclipse构建maven的web项目
查看>>
深入浅出MyBatis:MyBatis的所有配置
查看>>
StringBuffer.append比String加号好在那里?反编译的思路很赞
查看>>
MBaaS-LiveOak系列一:LiveOak简介
查看>>
【2016-03-12】HBase、Redis、Mongodb比较(未完待续)
查看>>
互联网公司:4家面试心得
查看>>
电脑常见问题及解决办法
查看>>
修改word2010的样式 快捷键
查看>>
Oracle树查询(start with connect by prior)探究
查看>>
前后端分离的验证码实现方案
查看>>
WannaCry?这才刚开始…
查看>>
perl通过CPAN安装DBD::Oracle时出错(2)
查看>>
导入maven项目出现 Unsupported IClasspathEntry kind=4
查看>>