Compare commits

1 Commits

Author SHA1 Message Date
261bb9d586 ajout atelier functions 2022-05-18 21:14:20 +02:00
4 changed files with 13 additions and 42 deletions

View File

@@ -2,12 +2,17 @@
### Exercice 1 ### Exercice 1
le script eval.sh présente des redondances de code qui le rende difficile à maintenir. Voir le fichier eval.sh
Dans l'idéal il faudra factoriser en créeant 2 fonctions Essayez d'identifier sa fonction
Identifiez ses lacunes
Proposez une correction
### Exercice 2 ### Exercice 2
Définir dans le fichier myfunctions.sh une fonction "myerror" prenant 2 arguments en entrée: un code et un message. Définir dans le fichier myfunctions.sh une fonction "myerror" prenant 2 arguments en entrée: un code et un message.
La fonction affiche le message donné et termine le script en renvoyant le code La fonction affiche le message donné et termine le script en renvoyant le code

30
eval.sh
View File

@@ -1,30 +1,12 @@
#! /bin/bash #! /bin/bash
IFS=$'\n\t' IFS=$'\n\t'
function fileMTime {
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
stat --printf "%y;" $1
}
function fileMustContains {
file=$1
shift
string=$@
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ;fi
if ( grep -i -qE "${string}" $file ) ;
then echo -ne "1;"
else
echo -ne "0;"
fi
}
host=$1 host=$1
root=${host}/rootfs root=${host}/rootfs
host=$(basename $host) host=$(basename $host)
hostname=$(grep HOSTNAME ${root}/etc/sysconfig/network 2>/dev/null| cut -f2 -d=) hostname=$(grep HOSTNAME ${root}/etc/sysconfig/network 2>/dev/null| cut -f2 -d=)
fileMTime ${root}/root/.bash_history stat --printf "%y;" ${root}/root/.bash_history
fileMTime ${root}/home/formation/.bash_history stat --printf "%y;" ${root}/home/formation/.bash_history
fileMTime ${root}/etc/motd stat --printf "%y;" ${root}/etc/motd
fileMustContains ${root}/etc/profile.d/zalias.sh "today" if( grep -i -qE "today" ${root}/etc/profile.d/zalias.sh ) ; then echo -ne "1;" ; else echo -ne "0;" fi
fileMustContains ${root}/etc/profile.d/zconf.sh "PS1" if( grep -i -qE "PS1" ${root}/etc/profile.d/zconf.sh ) ; then echo -ne "1;" ; else echo -ne "0;" fi
fileMustContains ${root}/etc/profile.d/zconf.sh "HISTSIZE" if( grep -i -qE "HISTSIZE" ${root}/etc/profile.d/zconf.sh ) ; then echo -ne "1;" ; else echo -ne "0;" fi

View File

@@ -1,7 +0,0 @@
function myerror {
code=$1
shift
msg="$*"
echo $msg
exit $code
}

View File

@@ -1,9 +0,0 @@
#! /bin/bash
set -e
set -u
source myfunctions.sh
myerror 42 "check \$? var"