LO14
School project : realize a task manager (crontab) in bash
tacheron.sh
#!/bin/bash path='/etc/tacheron' function parse { # echo "DEBUG: $1" # L'erreur la plus bizarre au monde : si on place # le \- après le 0-9 ça ne marche plus ! if echo "$1" | grep --quiet ^[\-0-9\,\*/~]*$;then if echo "$1" | grep --quiet ^\*\/ ;then # echo "DEBUG: */ détecté" return 2 elif echo "$1" | grep --quiet ^\* ;then # echo "DEBUG: * détecté" return 1 elif echo "$1" | grep --quiet \-;then # echo "DEBUG: - détecté" return 3 elif echo "$1" | grep --quiet \,;then # echo "DEBUG: , détecté" return 4 else return 5 fi else return 6 fi } supprVal="" function getTilde { supprVal="" supprVal=$(echo "$1" | cut --delimiter="~" --output-delimiter=" " -f2-) # echo "DEBUG: contenu de supprVal=${supprVal}" } interval="" function getInterval { # Cette fonction va parser le champ donné # Gestion des intervalles avec possibilité de suppresion : # On renvoit le code 0 et la liste des valeurs # (retours à la lignes entre) est dans une variable globale # Gestion des listes (séparation par virgule) : # On renvoit le code 0 et la liste des valeurs # est contenue dans une variable globale # Gestion des champs étoilés : # On renvoit le code 1 ou sinon la valeur du diviseur # Remarque : la valeur du diviseur peut-être de 1 # cela revient à un * simple parse "$1" resultat=$? interval="" supprVal="" # Cas d'un champ intervalle if [ ${resultat} -eq 3 ];then premiereVal=$(echo "$1" | cut --delimiter="-" -f1) derniereVal=$(echo "$1" | cut --delimiter="-" -f2 | cut --delimiter="~" -f1) if echo "$1" | grep --quiet \~;then getTilde "$1" fi for((i=${premiereVal};i> /var/log/tacheron fi else # echo "DEBUG: Erreur de syntaxe" echo "$(date) - ${username} - Erreur de syntaxe pour la commande $ch7" >> /var/log/tacheron fi done #else # echo "DEBUG: utilisateur ${username} non-autorisé." fi done IFS=${SAVEIFS} temps=$(echo "$temps - ($(date +%s) - ${starttime})" | bc) # echo "DEBUG: Sleeping pendant ${temps}" sleep "${temps}" done fi
tacherontab.sh
#!/bin/bash path='/etc/tacheron' actionUser=$USER action=0 while getopts "u:lre" opts;do case $opts in u) echo "DEBUG: Utilisateur $OPTARG" actionUser=$OPTARG ;; l) echo "DEBUG: Option l" action=1 ;; r) echo "DEBUG: Option r" action=2 ;; e) echo "DEBUG: Option e" action=3 ;; esac done echo "DEBUG: ${action} pour ${actionUser}" case ${action} in 1) echo "DEBUG: list" if [ -f ${path}/tacheron${actionUser} ];then echo "DEBUG: le fichier ${path}/tacheron${actionUser} va être affiché" cat ${path}/tacheron${actionUser} else echo "Le fichier ${path}/tacheron${actionUser} n'existe pas" fi ;; 2) echo "DEBUG: remove" if [ -f ${path}/tacheron${actionUser} ];then echo "DEBUG: le fichier ${path}/tacheron${actionUser} va être supprimé" rm ${path}/tacheron${actionUser} else echo "Le fichier ${path}/tacheron${actionUser} n'existe pas" fi ;; 3) echo "DEBUG: edit" if [ -f ${path}/tacheron${actionUser} ];then echo "DEBUG: le fichier ${path}/tacheron${actionUser} va être ouvert" cp ${path}/tacheron${actionUser} /tmp/tacheron${actionUser} else echo "DEBUG: Le fichier ${path}/tacheron${actionUser} n'existe pas : création" fi vi /tmp/tacheron${actionUser} if [ -s /tmp/tacheron${actionUser} ];then cp /tmp/tacheron${actionUser} ${path}/tacheron${actionUser} else echo "Action annulée" fi ;; esac