2017-03-23 2 views
1

Script bash rapide pour awstats. Notant beau, mais ayant un problème avec la boucle ne se produisant pas. La sortie montre seulement qu'elle a effectué une boucle pour le nom 1, elle n'a jamais obtenu le nom 2, le nom 3 avec le printf.Boucle Bash avec des instructions If qui ne se bouclent pas correctement

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

'exec' en est la cause. Pourquoi avez-vous besoin de 'exec'? – codeforester

+1

Regardez s'il vous plaît: http://www.shellcheck.net/ – Cyrus

+0

Vous utilisez des guillemets simples lorsque vous devriez utiliser des citations retour (ou mieux, '$ (...)') dans vos 5 premières lignes. – chepner

Répondre

1
exec /usr/bin/gzip -cv "$activelogpath"/"$i"/*.log > "$archivelogpath"/"$i"/"$(date +%Y%m%d_%H%M%S)".gz 

exec remplace le processus en cours avec le programme nommé. L'exécution ne continue pas après une instruction exec. Il n'y a pas besoin d'ici. Il suffit d'appeler gzip sans elle.

gzip -cv "$activelogpath/$i"/*.log > "$archivelogpath/$i/$(date +%Y%m%d_%H%M%S).gz" 

Il y a aussi pas besoin d'écrire /usr/bin/, ou de quitter et entrer des citations si souvent.