1、判断nginx是否在运行
示例一
this_pid=$$ //当前进程号
while true; do
ps -ef|grep nginx|grep -v grep|grep -v $this_pid
if [ $? -eq 0 ]; then
echo "Nginx is running..."
sleep 3
else
systemctl start nginx
echo "Nginx is down, starting..."
fi
done
示例二
# return返回数字
this_pid=$$
function is_nginx_running{
ps -ef|grep nginx|grep -v grep|grep -v $this_pid &> /dev/null
if [ $? -eq 0 ]; then
return
else
return 1
fi
}
is_nginx_running && echo "Nginx is runnning" || echo "Nginx is stopped"
2、获取passwd用户信息
#echo返回列表
function get_sys_user{
users=`cat passwd | cut -d ":" -f 1`
echo $users
}
index=1
user_list=`get_sys_user`
for u in user_list; do
echo "This is $index user: $u"
index=$(($index+1))
done
3、加减乘除
function add{
echo `expr $1 + $2`
}
function reduce{
echo `expr $1 - $2`
}
function multiple{
echo `expr $1 \* $2`
}
function divide{
echo `expr $1 / $2`
}