函數的返回值兩種形式
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
