.scriptDiff.sh
Jump to navigation
Jump to search
#!/bin/sh
# ["srv1 srv2 ... srvn"] "codeFile"
[ $# -eq 0 ] &&exit
# initialisation des variables
baseDir="$(realpath "$(dirname $0)"/..)"
cacheFile=/tmp/"$(basename $0)"$$
# stock code en cache avec des accès limités
cat "${@: -1}" >"${cacheFile}"
chmod 500 "${cacheFile}"
chown infocepo: "${cacheFile}"
if [ $# -eq 1 ] ;then
serverCheck=$(hostList.sh)
elif [ $# -ge 2 ] ;then
serverCheck=$(hostList.sh $1)
fi
if [ $# -ge 3 ] ;then
hostList=$(hostList.sh "${@:2:$(($# -2))}")
else
hostList=$serverCheck
fi
# signature du code
nameBase="$(cat ${cacheFile} |tr -c '[:alnum:]' '_' |cut -c 1-68 |tr -s '_' |sed -r 's#_$##' )"-$(md5sum ${cacheFile} |cut -c 1-2 )-$(date '+%Y%m%d')
resultName="${baseDir}"/log/$(basename $0 ).$(echo "${serverCheck}" |tr -c '[:alnum:]' '_' |cut -c 1-8).$(echo "${serverCheck}" |sha512sum |cut -c 1-2).$(cat "${cacheFile}" |tr -c '[:alnum:]' '_' |cut -c 1-8).$(echo "${serverCheck} ${hostList}" |sha512sum | cut -c 1-2)."${nameBase}".gz
# lancement du code
## Enleve les lignes en doublons par serveur. Puis ne garde que les lignes uniques entre tous les serveurs.
.scriptComm.sh "${hostList}" "${cacheFile}" 2>/dev/null >"${cacheFile}".Comm
for serverMy in $serverCheck ;do
script.sh "${serverMy}" "${cacheFile}" 2>/dev/null \
| awk '!seen[$0]++' >"${cacheFile}".result
if [ -z "$(cat "${cacheFile}".result )" ] ;then
echo "== ${serverMy} no result =="
else
( cat "${cacheFile}".result
echo "=== ${serverMy} missing ==="
cat "${cacheFile}".Comm
) |grep . >"${cacheFile}".result2
awk 'NR == FNR {count[$0]++; next}; count[$0] == 1' "${cacheFile}".result2 "${cacheFile}".result2 >"${cacheFile}".result
cat "${cacheFile}".result |sed '/=== /,$d' >"${cacheFile}".head
cat "${cacheFile}".result |sed '/=== /,$!d' >"${cacheFile}".tail
[ "$(cat "${cacheFile}".head |grep . |wc -l )" -ge 2 ] &&cat "${cacheFile}".head
[ "$(cat "${cacheFile}".tail |grep . |wc -l )" -ge 2 ] &&cat "${cacheFile}".tail
fi
done \
| gzip>"${cacheFile}".Diff.gz
# mise en cache
mv "${cacheFile}".Diff.gz "${resultName}"
# affiche le resultat
zcat "${resultName}"
# clean cache
rm -f /tmp/"$(basename $0)"$$* 2>/dev/null