Conversion en csv. formattage deléguée à column

ajout fonctions verif user, group et mtime d'un fichier
This commit is contained in:
2021-02-08 14:31:14 +01:00
parent 33d738a69f
commit baf32ddcf9

View File

@@ -1,14 +1,14 @@
#! /bin/bash
function e {
echo -ne "${1}\t"
echo -ne "${1};"
}
# file mtime
# arg1: file
function fileMTime {
if [ ! -f $1 ] ; then echo -ne "0\t" ; return 0 ; fi
stat --printf "%y\t" $1
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
stat --printf "%y;" $1
}
@@ -19,46 +19,46 @@ function exitCodeOk {
program=$1
code=${2:=0}
$program &> /dev/null
if [ $? -eq $code ] ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
if [ $? -eq $code ] ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
#Ok if file size > X
# arg1: file
# arg2: min size
function fileBiggerThan {
if [ ! -f $1 ] ; then echo -ne "0\t" ; return 0 ; fi
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
filesize=$(stat --format "%s" $1)
if [ $filesize -lt $2 ] ; then echo -ne "0\t" ; else echo -ne "1\t" ; fi
if [ $filesize -lt $2 ] ; then echo -ne "0;" ; else echo -ne "1;" ; fi
}
#Ok if file exists
# arg1: file to check
function fileMustExist {
if [ -f ${1} ] ; then echo -ne "1\t"
else echo -ne "0\t"
if [ -f ${1} ] ; then echo -ne "1;"
else echo -ne "0;"
fi
}
#Ok if file DOESNOT exists
# arg1: file to check
function fileMustNOTExist {
if [ -f ${1} ] ; then echo -ne "0\t"
else echo -ne "1\t"
if [ -f ${1} ] ; then echo -ne "0;"
else echo -ne "1;"
fi
}
#Ok if dir exists
# arg1: dir to check
function dirMustExist {
if [ -d ${1} ] ; then echo -ne "1\t"
else echo -ne "0\t"
if [ -d ${1} ] ; then echo -ne "1;"
else echo -ne "0;"
fi
}
# Ok if given dir DOESNOT exists
# arg1: dir
function dirMustNOTExist {
if [ -d ${1} ] ; then echo -ne "0\t"
else echo -ne "1\t"
if [ -d ${1} ] ; then echo -ne "0;"
else echo -ne "1;"
fi
}
@@ -69,11 +69,11 @@ function fileMustContain {
file=$1
shift
string=$@
if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ;fi
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ;fi
if ( grep -i -qE "${string}" $file ) ;
then echo -ne "1\t"
then echo -ne "1;"
else
echo -ne "0\t"
echo -ne "0;"
fi
}
@@ -84,9 +84,9 @@ function fileCountPattern {
file=$1
shift
string=$@
if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ;fi
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ;fi
num=$(grep -ciE "${string}" $file )
echo -ne "$num\t"
echo -ne "$num;"
}
# Ok if file contains N pattern
@@ -98,12 +98,12 @@ function fileMustContainNItem {
n=$2
shift
string=$@
if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ;fi
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ;fi
num=$(grep -ciE "${string}" $file )
if [ $num -eq $1 ] ;
then echo -ne "1\t"
then echo -ne "1;"
else
echo -ne "0\t"
echo -ne "0;"
fi
}
@@ -114,30 +114,30 @@ function fileMustNOTContain {
file=$1
shift
string=$@
if [ ! -f $file ] ; then echo -ne "0\t" ; return 0 ; fi
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ; fi
if ( grep -i -qE "${string}" $file ) ;
then echo -ne "0\t"
then echo -ne "0;"
else
echo -ne "1\t"
echo -ne "1;"
fi
}
# Ok if given user exists
# Arg1: user
function userExists {
if ( grep -iq $1 /etc/passwd ) ; then e 1 ; else e0 ; fi
if ( grep -iq $1 /etc/passwd ) ; then e 1 ; else e 0 ; fi
}
# Ok if given group exists
# Arg1: group
function groupExists {
if ( grep -iq $1 /etc/group ) ; then e 1 ; else e0 ; fi
if ( grep -iq $1 /etc/group ) ; then e 1 ; else e 0 ; fi
}
# insert hostname at beginning of result line
function addHeader {
test -f /etc/motd && user="$(cat /etc/motd | sed 's/\n//')\t"
echo -ne "${user}$(hostname -s)\t"
test -f /etc/motd && user="$(cat /etc/motd | sed 's/\n//');"
echo -ne "${user}$(hostname -s);"
}
# Ok if given package is installed
@@ -145,7 +145,7 @@ function addHeader {
function pkgInstalled {
pkglist=/tmp/pkg.list
test -f $pkglist || rpm -qa > $pkglist
if ( grep -qi $1 $pkglist ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
if ( grep -qi $1 $pkglist ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
# Ok if given package is NOT installed
@@ -153,20 +153,20 @@ function pkgInstalled {
function pkgNotInstalled {
pkglist=/tmp/pkg.list
test -f $pkglist || rpm -qa > $pkglist
if ( grep -qiE $1 $pkglist ) ; then echo -ne "0\t" ; else echo -ne "1\t" ; fi
if ( grep -qiE $1 $pkglist ) ; then echo -ne "0;" ; else echo -ne "1;" ; fi
}
# ok if given process is running
# arg1: process to check
function processIsRunning {
if ( pgrep -f $1 &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
if ( pgrep -f $1 &> /dev/null ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
#Ok if given command returns 0
# arg1: command to be run
function commandIsWorking {
command=$1
if ( $command &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
if ( $command &> /dev/null ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
# ok if given pattern is detected on command's stdout or stderr
@@ -176,21 +176,21 @@ function commandStdoutPattern {
command=$1
shift
pattern=$@
if ( $command 2>&1 | grep -qiE "${pattern}" ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
if ( $command 2>&1 | grep -qiE "${pattern}" ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
# ok if given service is enabled
# arg1: service
function serviceIsEnabled {
service=$1
if ( systemctl is-enabled $1 &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
if ( systemctl is-enabled $1 &> /dev/null ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
# ok if given service is active
# arg1: service
function serviceIsActive {
service=$1
if ( systemctl is-active $1 &> /dev/null ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
if ( systemctl is-active $1 &> /dev/null ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
# ok if output of given url contains given stuff
@@ -199,5 +199,5 @@ function serviceIsActive {
function okIfCurl {
url=$1
shift
if ( curl -q "${url}" | grep -q "$@" ) ; then echo -ne "1\t" ; else echo -ne "0\t" ; fi
if ( curl -q "${url}" | grep -q "$@" ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}