Shell自動化
緣由:
因為我做好404頁面後,要放進將近40個子網域下。簡直要命,於是我寫了一個shell來讓我更輕鬆實施。目標:
自動化,放進將近40個子網域下。程式碼:
#!/bin/bash
#PATH:~
# Program:auto copy 404Error
# History:
# 2017/07/19
cd /home
dirname=`dirname /home/`
#echo $dirname;
x=0;
for dirname in *;
do
array[$x]=$dirname
getDir=${array[$x]}; #傳陣列單一元素,至 getDir 變數
if [ -f "$getDir" ]; then # -f 是否存在且為檔案(file)
let "x+=1"
continue
fi
cd /home/$getDir/public_html
#let "x+=1"
if [ -f "forum.php" ]; then # forum.php 是個別子網域下論壇的重要根據
# 檔案 /path/to/dir/filename 存在
echo "File /path/to/dir/filename exists."
if [ -f "404.shtml" ]; then
echo "exits."
else
cp /home/404.shtml ./ && cp /home/.htaccess ./
chown $getDir:$getDir ./404.shtml && chown $getDir:$getDir ./.htaccess
echo "Add OK"
fi
else
# 檔案 /path/to/dir/filename 不存在
echo "File /path/to/dir/filename does not exists."
fi
let "x+=1"
done
#echo ${array[@]};#列出陣列所有元素
#echo ${#array[@]};#列出有幾個元素
總節:上述是有限制性的,需讀者自行去變化。也許有BUG需要讀者的發現,但我測試是沒問題的。如有更好的方式歡迎分享。參考資料:
http://jax-work-archive.blogspot.tw/2011/04/shell-if.html
http://www.it610.com/article/1088051.htm
https://www.phpini.com/linux/shell-script-get-dirname-filename

留言
張貼留言