shell编程:函数的返回值

函数的返回值两种形式

return 0-255 0表示成功,1-255表示失败-------通常用于判断

echo 返回一个字符串------------通常用于返回一个执行的结果

return.sh

#!/bin/bash
#

this_pid=$$

function nginxck

{
        ps -ef | grep nginx | grep -v grep | grep -v $this_pid | &> /dev/null
        if [ $? -eq 0 ];then
              return 0
        else
              return 1
        fi
}

nginxck && echo "nginx is running" || echo "nginx is stoped"

echo.sh

#!/bin/bash
#

function get_users

{
users=`cat /etc/passwd | cut -d ":" -f 1`
echo $users
}

user_list=`get_users`
index=1

for u in $user_list
do
echo "the $index user is : $u"
index=`expr $index + 1`
done

猜你喜欢

转载自www.cnblogs.com/soymilk2019/p/11730030.html