1、不带参数的函数
#!/bin/bash # 定义函数,可以不用function开头 function checkhttpd() { check=`netstat -tnlp | grep httpd | wc -l` echo $check } function uphttpd() { service httpd start } #可以用 `checkhttpd`或 $(checkhttpd) 将函数执行结果赋值给变量 #如 a=`checkhttpd` 或 a=$(checkhttpd) echo "now status is:" `checkhttpd` while [ `checkhttpd` == 0 ] do echo "start apache" uphttpd sleep 10 done
2、带参数的函数
#!/bin/bash function aaa() { echo 'first parameter is:' $1 echo 'second parameter is:' $2 } # 直接函数名 后面用空格加参数 aaa 11 22
3、带return的函数
#!/bin/bash #需要注意,带return的函数,不能赋值给变量,出不来结果,需要用$?来获取执行结果 function a() { return 123 } a echo $? #aa=`a` #echo $aa 这种方式无法使用
原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/3687.html