Bash 使用 If 语句循环不正确
Bash Loop with If Statements not Looping Properly
awstats 的快速 bash 脚本。注意到很漂亮,但是没有出现循环问题。输出仅显示它已为 name1 执行了一个循环,它永远不会使用 printf 到达 name2、name3。
#!/bin/bash
awstats_command='perl /var/www/html/awstats/wwwroot/cgi-bin/awstats.pl'
html_path="/var/www/html/awstats/wwwroot"
activelogpath="/logs/web/active"
archivelogpath="/logs/web/archive"
day='date +%Y-%m-%d.%H'
# List of web servers we are processing stats for:
for i in name1 name2 name3
do
if [[ $i = "name1" ]]
then
# Custom reports for name1 contains subdirectory statistics
printf "\nProcessing log files for $i...\n"
/usr/bin/perl /var/www/html/awstats/wwwroot/cgi-bin/awstats.pl -config=name1 -update
printf "done.\n"
printf "\nGenerating .html files for $i...\n"
/var/www/html/awstats/wwwroot/cgi-bin/do.reports $i
$awstats_command -config=$i -output=urldetail:/about/ -staticlinks > $html_path/$i/awstats.$i.about.html
printf "done.\n"
else
printf "\nProcessing log files for $i...\n"
# Will do something when working $i
printf "done.\n"
printf "\nGenerating .html files for $i...\n"
# Will do something when working $i
printf "done.\n"
fi
printf "\nCompressing and archiving log files...\n"
exec /usr/bin/gzip -cv "$activelogpath"/"$i"/*.log > "$archivelogpath"/"$i"/"$(date +%Y%m%d_%H%M%S)".gz
# rm -f $activelogpath/$i/*.log
printf "\nCompleted!\n"
done
exec /usr/bin/gzip -cv "$activelogpath"/"$i"/*.log > "$archivelogpath"/"$i"/"$(date +%Y%m%d_%H%M%S)".gz
exec
用命名程序替换当前进程。执行不会继续超过 exec
语句。这里没有必要。没有它就调用 gzip
。
gzip -cv "$activelogpath/$i"/*.log > "$archivelogpath/$i/$(date +%Y%m%d_%H%M%S).gz"
也没有必要写 /usr/bin/
,或如此频繁地离开并重新输入引号。
awstats 的快速 bash 脚本。注意到很漂亮,但是没有出现循环问题。输出仅显示它已为 name1 执行了一个循环,它永远不会使用 printf 到达 name2、name3。
#!/bin/bash
awstats_command='perl /var/www/html/awstats/wwwroot/cgi-bin/awstats.pl'
html_path="/var/www/html/awstats/wwwroot"
activelogpath="/logs/web/active"
archivelogpath="/logs/web/archive"
day='date +%Y-%m-%d.%H'
# List of web servers we are processing stats for:
for i in name1 name2 name3
do
if [[ $i = "name1" ]]
then
# Custom reports for name1 contains subdirectory statistics
printf "\nProcessing log files for $i...\n"
/usr/bin/perl /var/www/html/awstats/wwwroot/cgi-bin/awstats.pl -config=name1 -update
printf "done.\n"
printf "\nGenerating .html files for $i...\n"
/var/www/html/awstats/wwwroot/cgi-bin/do.reports $i
$awstats_command -config=$i -output=urldetail:/about/ -staticlinks > $html_path/$i/awstats.$i.about.html
printf "done.\n"
else
printf "\nProcessing log files for $i...\n"
# Will do something when working $i
printf "done.\n"
printf "\nGenerating .html files for $i...\n"
# Will do something when working $i
printf "done.\n"
fi
printf "\nCompressing and archiving log files...\n"
exec /usr/bin/gzip -cv "$activelogpath"/"$i"/*.log > "$archivelogpath"/"$i"/"$(date +%Y%m%d_%H%M%S)".gz
# rm -f $activelogpath/$i/*.log
printf "\nCompleted!\n"
done
exec /usr/bin/gzip -cv "$activelogpath"/"$i"/*.log > "$archivelogpath"/"$i"/"$(date +%Y%m%d_%H%M%S)".gz
exec
用命名程序替换当前进程。执行不会继续超过 exec
语句。这里没有必要。没有它就调用 gzip
。
gzip -cv "$activelogpath/$i"/*.log > "$archivelogpath/$i/$(date +%Y%m%d_%H%M%S).gz"
也没有必要写 /usr/bin/
,或如此频繁地离开并重新输入引号。