bash的数值代表和其它代表相反:0表示true;非0代表false。
#!/bin/sh
PIDFILE="pid"
# true=nginx进程运行 false=nginx进程未运行
checkRunning(){
# -f true表示普通文件
if [ -f "$PIDFILE" ]; then
# -z 字符串长度为0true
if [ -z "`cat $PIDFILE`" ];then
echo "ERROR: Pidfile '$PIDFILE' exists but contains no pid"
return "1";
fi
PID="`cat ${PIDFILE}`"
RET="`ps -p "${PID}"|grep nginx`"
# -n 字符串长度不为0为true
if [ -n "$RET" ];then
echo "${RET}"
return "0";
fi
fi
return "1";
}
checkT_F(){
return 2
}
status(){
if (checkRunning);then
PID="`cat $PIDFILE`"
echo "'$SERVICE_NAME' is running (pid '$PID')"
exit 0
fi
echo "'$SERVICE_NAME' not running"
exit 1
}
case "$1" in
status)
status
;;
zz)
if [ -z "123" ];then
echo "123 lenhth is 0"
else
echo "123 length is not 0"
fi
;;
check)
if checkT_F ;then
echo '2 is true'
else
echo '2 is flase'
fi
;;
*)
esac