如果语句没有正确循环,则使用Bash循环

时间:2017-03-23 18:27:27

标签: bash loops if-statement exec awstats

awstats的快速bash脚本。注意到漂亮,但有一个循环问题而不是ocurring。输出只显示它为name1执行了一个循环,它永远不会到达name2,name3和printf。

#!/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

1 个答案:

答案 0 :(得分:2)

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/,或者经常离开并重新输入引号。