code-test

2015年8月25日 星期二

shell script 檢查目錄,沒有目錄並建立

#!/bin/sh
#shell判斷檔,目錄是否存在或者具有許可權
Path="/var/log/httpd/"
File="/var/log/httpd/access.log"

#這裡的-x 參數判斷$Path是否存在並且是否具有可執行許可權
if [ ! -x "$Path"]; then
mkdir "$Path"
fi

#這裡的-d 參數判斷$Path是否存在
if [ ! -d "$Path"]; then
mkdir "$Path"
fi

#這裡的-f參數判斷$File是否存在
if [ ! -f "$File" ]; then
touch "$File"
echo "檢查資料檔案不存在已建立"
fi

#其他參數還有-n,-n是判斷一個變數是否是否有值
if [ ! -n "$Var" ]; then
echo "$Var is empty"
exit 0
fi

#兩個變數判斷是否相等
if [ "$var1" = "$var2" ]; then
echo '$var1 eq $var2'
else
echo '$var1 not eq $var2'
fi

echo "程式繼續...."

/etc/crontab 內容
10 0 * * * root /usr/sbin/ntpdate time.stdtime.gov.tw; /usr/sbin/hwclock -w
40 5 * * * root yum -y update && yum clean packages

沒有留言:

張貼留言