Shell if else语句实战案例
原创
©著作权归作者所有:来自51CTO博客作者wx5bcd2f496a1cf的原创作品,请联系作者获取转载授权,否则将追究法律责任
判断文件,目录是否存在
-d filename 如果 filename为目录,则为真
-f filename 如果 filename为常规文件,则为真
目录:
path="/home"
#if [ ! -d ${path} ];then
if [ -d ${path} ];then
echo dir ${path} exist!
else
echo dir ${path} not exist!
fi
文件:
file="/home/log.txt"
if [ -f ${file} ];then
echo file ${file} exist!
else
echo file ${file} not exist!
fi
Nginx日志切割
#!/bin/bash
#auto mv nginx log shell
#by author test
S_LOG=/usr/local/nginx/logs/access.log
D_LOG=/data/backup/`date +%Y%m%d`
echo -e "\033[32mPlease wait start cut shell scripts...\033[1m"
sleep 2
if [ ! -d $D_LOG ];then
mkdir -p $D_LOG
fi
mv $S_LOG $D_LOG
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
echo "-------------------------------------------"
echo "The Nginx log Cutting Successfully!"
echo "You can access backup nginx log $D_LOG/access.log files."
shell脚本检测Nginx服务是否正常启动,如果宕机,那么就设置邮箱报警
#!/bin/bash
nx=`netstat -tpln | grep nginx | wc -l` #用这条命令也可以ps -ef | grep nginx | grep -v grep | grep master | wc -l
if [ $nx -eq 0 ];then
/usr/local/nginx/sbin/nginx
fi
if [ $? -ne 0 ];then
echo "nginx is down send mail" #如果nginx启动失败发送邮件,这里邮件可以自己配置
fi