# Comment alléger ce script?

**URL:** <https://www.debian-fr.org/t/comment-alleger-ce-script/24847>\
**Category:** Programmation\
**Created:** [Février 20, 2016, 9:31 UTC](https://www.debian-fr.org/t/comment-alleger-ce-script/24847 "2016-02-20T21:31:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![thuban](https://www.debian-fr.org/user_avatar/www.debian-fr.org/thuban/32/4177_2.png) [@thuban](https://www.debian-fr.org/u/thuban)\
**Post date:** [Février 20, 2016, 9:31 UTC](https://www.debian-fr.org/t/comment-alleger-ce-script/24847/1 "2016-02-20T21:31:57Z")

</div>

Bonsoir!  
j’ai écrit un script permettant de savoir combien de nouveaux mails j’ai sur mes boites. Cependant, il est sûrement un peu lourd. Pourriez-vous me le corriger et me dire comment l’améliorer?  
le voici :

[code]#!/bin/sh  
#script pour vérifier mes mails sur les 2 boites.  
#Le fichier de configuration est ~/.fetchmail/fetchmailrc

#On vérifie que le script ne tourne pas déjà  
\_PS=`ps aux`  
\_PID=`echo "${_PS}" | grep $0 | tr -s [:space:] | cut -d' ' -f2`  
\_LINES=`echo "${_PID}" | wc -w`

if [${\_LINES} -gt 1]; then  
\_PID=`echo ${_PID} | cut -d' ' -f1`  
echo "Un script en trop, on le kill (pid: ${\_PID})"  
kill ${\_PID}  
fi

while true; do  
TEST=$(ping -c1 [www.google.fr](http://www.google.fr) | grep received | awk ‘{print $5}’)

if [-e /tmp/mails]; then  
rm /tmp/mails  
fi

if [“$TEST” = “received,”]; then  
TOTGMAIL="$(fetchmail -c -p IMAP [imap.gmail.com](http://imap.gmail.com) --port 993 --ssl --username xavier.cartron -f .fetchmail/fetchmailrc | tail -n 1 | awk ‘{print $1}’)“  
LUGMAIL=”$(fetchmail -c -p IMAP [imap.gmail.com](http://imap.gmail.com) --port 993 --ssl --username xavier.cartron -f .fetchmail/fetchmailrc | tail -n 1 | awk ‘{print $3}’ | sed ‘s/(//’)“  
NGMAIL=$(($TOTGMAIL - $LUGMAIL))  
TOTFAC=”$(fetchmail -c -p IMAP [imaps.etu.univ-nantes.fr](http://imaps.etu.univ-nantes.fr) --port 993 --ssl --username e072767U -f .fetchmail/fetchmailrc | tail -n 1 | awk ‘{print $1}’)“  
LUFAC=”$(fetchmail -c -p IMAP [imaps.etu.univ-nantes.fr](http://imaps.etu.univ-nantes.fr) --port 993 --ssl --username e072767U -f .fetchmail/fetchmailrc | tail -n 1 | awk ‘{print $3}’ | sed ‘s/(//’)"  
NFAC=$(($TOTFAC - $LUFAC))

echo -n " | Gmail:$NGMAIL-" \>\> /tmp/mails  
echo -n "Fac:$NFAC | " \>\> /tmp/mails

else  
echo " | Mails:??? | " \> /tmp/mails

fi  
sleep 300  
done  
else  
exit  
[/code]

---

<div class="post-metadata">

**Author:** ![kna](https://www.debian-fr.org/user_avatar/www.debian-fr.org/kna/32/5623_2.png) [@kna](https://www.debian-fr.org/u/kna)\
**Post date:** [Février 20, 2016, 9:32 UTC](https://www.debian-fr.org/t/comment-alleger-ce-script/24847/2 "2016-02-20T21:32:06Z")

</div>

Première réflexion : si tu indentes le code, il paraîtra moins lourd !

Sinon, pour le code, il est bien. Tu pourrais à la rigueur faire deux fetchmailrc, pour éviter d’avoir à indiquer le compte que tu veux vérifier dans les options de fetchmail. Et enregistrer le résultat de fetchmail -c dans une variable pour éviter de le lancer deux fois. À la louche, ça donnerait :

[code]#!/bin/sh  
#script pour vérifier mes mails sur les 2 boites.  
#Le fichier de configuration est ~/.fetchmail/fetchmailrc

TEST=$(ping -c1 [www.google.fr](http://www.google.fr) | grep received | awk ‘{print $5}’)  
while true; do  
if [“$TEST” = “received,”]; then  
RESULTGMAIL = $(fetchmail -c -f .fetchmail/fetchmailrc\_gmail)  
RESULTFAC = $(fetchmail -c -f .fetchmail/fetchmailrc\_fac)

```
  TOTGMAIL="$(echo $RESULTGMAIL | tail -n 1 | awk '{print $1}')"
  LUGMAIL="$(echo $RESULTGMAIL | tail -n 1 | awk '{print $1}' | sed 's/(//')"
  NGMAIL=$(($TOTGMAIL - $LUGMAIL))

  TOTFAC="$(echo $RESULTFAC | tail -n 1 | awk '{print $1}')"
  LUFAC="$(echo $RESULTFAC | tail -n 1 | awk '{print $1}' | sed 's/(//')"
  NFAC=$(($TOTFAC - $LUFAC))

  echo -n "Gmail : $NGMAIL" >> /tmp/dstatus
  echo -n "Fac : $NFAC" >> /tmp/dstatus

```

else  
echo " ???" \> /tmp/dstatus

fi  
sleep 300  
done  
exit[/code]

---

<div class="post-metadata">

**Author:** ![thuban](https://www.debian-fr.org/user_avatar/www.debian-fr.org/thuban/32/4177_2.png) [@thuban](https://www.debian-fr.org/u/thuban)\
**Post date:** [Février 20, 2016, 9:32 UTC](https://www.debian-fr.org/t/comment-alleger-ce-script/24847/3 "2016-02-20T21:32:10Z")

</div>

Merci pour la réponse c’est vraiment gentil! Je vais arranger ça, et ça sera un peu mieux! 🙂 :smt055

---

<div class="post-metadata">

**Author:** ![BBT1](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/b/ecc23a/32.png) [@BBT1](https://www.debian-fr.org/u/BBT1)\
**Post date:** [Février 20, 2016, 9:32 UTC](https://www.debian-fr.org/t/comment-alleger-ce-script/24847/4 "2016-02-20T21:32:10Z")

</div>

On peut factoriser les | tail -n 1 | awk ‘{print $5}’ pour faire moins de calculs et stockers des données de taille moins importante dans les variables :

[code]#!/bin/sh  
#script pour vérifier mes mails sur les 2 boites.  
#Le fichier de configuration est ~/.fetchmail/fetchmailrc

TEST=$(ping -c1 [www.google.fr](http://www.google.fr) | grep received | awk ‘{print $5}’)  
while true; do  
if [“$TEST” = “received,”]; then  
RESULTGMAIL = $(fetchmail -c -f .fetchmail/fetchmailrc\_gmail | tail -n 1 | awk ‘{print $1}’)  
RESULTFAC = $(fetchmail -c -f .fetchmail/fetchmailrc\_fac | tail -n 1 | awk ‘{print $1}’)

```
  TOTGMAIL="$RESULTGMAIL"
  LUGMAIL="$(echo $RESULTGMAIL | sed 's/(//' )"
  NGMAIL=$(($TOTGMAIL - $LUGMAIL))

  TOTFAC="$RESULTFAC"
  LUFAC="$(echo $RESULTFAC | sed 's/(//' )"
  NFAC=$(($TOTFAC - $LUFAC))

  echo -n "Gmail : $NGMAIL" >> /tmp/dstatus
  echo -n "Fac : $NFAC" >> /tmp/dstatus

```

else  
echo " ???" \> /tmp/dstatus

fi  
sleep 300  
done  
exit[/code]

---

<div class="post-metadata">

**Author:** ![thuban](https://www.debian-fr.org/user_avatar/www.debian-fr.org/thuban/32/4177_2.png) [@thuban](https://www.debian-fr.org/u/thuban)\
**Post date:** [Février 20, 2016, 9:32 UTC](https://www.debian-fr.org/t/comment-alleger-ce-script/24847/5 "2016-02-20T21:32:10Z")

</div>

Mais ça ne fonctionnera pas ici, car le nombre de mails lu n’est pas le même que le nombre de mails non lu. Donc on ne peut pas écrire comme tu le mets. (oui, je m’étais planté lors dela copie au premier post, j’ai corrigé ensuite.)  
Voila ce que ça me donne maintenant :

[code]#!/bin/sh  
#script pour vérifier mes mails sur les 2 boites.  
#Le fichier de configuration est ~/.fetchmail/fetchmailrc

#On vérifie que le script ne tourne pas déjà  
\_PS=`ps aux`  
\_PID=`echo "${_PS}" | grep $0 | tr -s [:space:] | cut -d' ' -f2`  
\_LINES=`echo "${_PID}" | wc -w`

if [${\_LINES} -gt 1]; then  
\_PID=`echo ${_PID} | cut -d' ' -f1`  
echo "Un script en trop, on le kill (pid: ${\_PID})"  
kill ${\_PID}  
fi

while true; do  
TEST=$(ping -c1 [www.google.fr](http://www.google.fr) | grep received | awk ‘{print $5}’)

```
if [-e /tmp/mails]; then
	rm /tmp/mails
fi

if ["$TEST" = "received,"]; then
	RESULTGMAIL=$(fetchmail -c -p IMAP imap.gmail.com --port 993 --ssl --username xavier.cartron -f .fetchmail/fetchmailrc | tail -n 1)
	RESULTFAC=$(fetchmail -c -p IMAP imaps.etu.univ-nantes.fr --port 993 --ssl --username e072767U -f .fetchmail/fetchmailrc | tail -n 1)
		TOTGMAIL="$(echo $RESULTGMAIL | awk '{print $1}')"
		LUGMAIL="$(echo $RESULTGMAIL | awk '{print $3}' | sed 's/(//')"
		NGMAIL=$(($TOTGMAIL - $LUGMAIL))
		TOTFAC="$(echo $RESULTFAC | awk '{print $1}')"
		LUFAC="$(echo $RESULTFAC | awk '{print $3}' | sed 's/(//')"
		NFAC=$(($TOTFAC - $LUFAC))

	echo -n " | Gmail:$NGMAIL-" >> /tmp/mails
	echo -n "Fac:$NFAC | " >> /tmp/mails

else
	echo " | Mails:??? | " > /tmp/mails

fi

```

sleep 300  
done  
exit  
[/code]

---

<div class="post-metadata">

**Author:** ![BBT1](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/b/ecc23a/32.png) [@BBT1](https://www.debian-fr.org/u/BBT1)\
**Post date:** [Février 20, 2016, 9:32 UTC](https://www.debian-fr.org/t/comment-alleger-ce-script/24847/6 "2016-02-20T21:32:12Z")

</div>

> [@](#):
>
> Mais ça ne fonctionnera pas ici, car le nombre de mails lu n’est pas le même que le nombre de mails non lu. Donc on ne peut pas écrire comme tu le mets.

OK, autant pour moi, j’ai peut être lu un peu vite 😉  
Mais bon, je vois que tu as retenu l’idée 🙂

---

<div class="post-metadata">

**Author:** ![thuban](https://www.debian-fr.org/user_avatar/www.debian-fr.org/thuban/32/4177_2.png) [@thuban](https://www.debian-fr.org/u/thuban)\
**Post date:** [Février 20, 2016, 9:32 UTC](https://www.debian-fr.org/t/comment-alleger-ce-script/24847/7 "2016-02-20T21:32:12Z")

</div>

oui merci, j’apprends petit à petit 🙂
