您的位置:首页 > 运维架构 > Linux

linux脚本学习指南--13 函数的定义和使用及带参函数

2019-06-17 10:27 337 查看

1.简单的函数定义

#! /bin/bash

function sayhello() {

echo "Hello,World"

}

sayhello    #函数调用

2.举个例子:写一个测试ngix进程是否起来,如果没有起来则手动启动nginx

检查nginx进程:ps -ef | grep nginx | grep -v grep

启动命令:systemctl start nginx

echo $?  #启动完成后函数返回值为0

vi nginx_daemon.sh

#! /bin/bash

#注意当脚本名称含有nginx关键子的时候 函数返回值是有问题的

this_pid=$$     #nginx启动的编号

ps -ef | grep nginx | grep -v grep | grep -v $this_pid &> /dev/null      #/dev/null 是Linux的一个无底洞

if [ $? -eq 0 ]; then             # $?脚本返回值

echo "nignx is well"

else 

systemtcl start nginx

echo "nginx is dowm.starting...."

fi

3.带参函数

 

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: