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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM