Shell if else語句實戰案例

判斷文件,目錄是否存在


-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."

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章