Comment alléger ce script?

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 | 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 --port 993 --ssl --username xavier.cartron -f .fetchmail/fetchmailrc | tail -n 1 | awk ‘{print $1}’)“
LUGMAIL=”$(fetchmail -c -p IMAP 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 --port 993 --ssl --username e072767U -f .fetchmail/fetchmailrc | tail -n 1 | awk ‘{print $1}’)“
LUFAC=”$(fetchmail -c -p IMAP 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]

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 | 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]

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

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 | 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]

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 | 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]

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

oui merci, j’apprends petit à petit :slightly_smiling: