Conversion en csv. formattage deléguée à column
ajout fonctions verif user, group et mtime d'un fichier
This commit is contained in:
74
functions.sh
74
functions.sh
@@ -1,14 +1,14 @@
|
|||||||
#! /bin/bash
|
#! /bin/bash
|
||||||
|
|
||||||
function e {
|
function e {
|
||||||
echo -ne "${1}\t"
|
echo -ne "${1};"
|
||||||
}
|
}
|
||||||
|
|
||||||
# file mtime
|
# file mtime
|
||||||
# arg1: file
|
# arg1: file
|
||||||
function fileMTime {
|
function fileMTime {
|
||||||
if [ ! -f $1 ] ; then echo -ne "0\t" ; return 0 ; fi
|
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
|
||||||
stat --printf "%y\t" $1
|
stat --printf "%y;" $1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -19,46 +19,46 @@ function exitCodeOk {
|
|||||||
program=$1
|
program=$1
|
||||||
code=${2:=0}
|
code=${2:=0}
|
||||||
$program &> /dev/null
|
$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
|
#Ok if file size > X
|
||||||
# arg1: file
|
# arg1: file
|
||||||
# arg2: min size
|
# arg2: min size
|
||||||
function fileBiggerThan {
|
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)
|
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
|
#Ok if file exists
|
||||||
# arg1: file to check
|
# arg1: file to check
|
||||||
function fileMustExist {
|
function fileMustExist {
|
||||||
if [ -f ${1} ] ; then echo -ne "1\t"
|
if [ -f ${1} ] ; then echo -ne "1;"
|
||||||
else echo -ne "0\t"
|
else echo -ne "0;"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
#Ok if file DOESNOT exists
|
#Ok if file DOESNOT exists
|
||||||
# arg1: file to check
|
# arg1: file to check
|
||||||
function fileMustNOTExist {
|
function fileMustNOTExist {
|
||||||
if [ -f ${1} ] ; then echo -ne "0\t"
|
if [ -f ${1} ] ; then echo -ne "0;"
|
||||||
else echo -ne "1\t"
|
else echo -ne "1;"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
#Ok if dir exists
|
#Ok if dir exists
|
||||||
# arg1: dir to check
|
# arg1: dir to check
|
||||||
function dirMustExist {
|
function dirMustExist {
|
||||||
if [ -d ${1} ] ; then echo -ne "1\t"
|
if [ -d ${1} ] ; then echo -ne "1;"
|
||||||
else echo -ne "0\t"
|
else echo -ne "0;"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
# Ok if given dir DOESNOT exists
|
# Ok if given dir DOESNOT exists
|
||||||
# arg1: dir
|
# arg1: dir
|
||||||
function dirMustNOTExist {
|
function dirMustNOTExist {
|
||||||
if [ -d ${1} ] ; then echo -ne "0\t"
|
if [ -d ${1} ] ; then echo -ne "0;"
|
||||||
else echo -ne "1\t"
|
else echo -ne "1;"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,11 +69,11 @@ function fileMustContain {
|
|||||||
file=$1
|
file=$1
|
||||||
shift
|
shift
|
||||||
string=$@
|
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 ) ;
|
if ( grep -i -qE "${string}" $file ) ;
|
||||||
then echo -ne "1\t"
|
then echo -ne "1;"
|
||||||
else
|
else
|
||||||
echo -ne "0\t"
|
echo -ne "0;"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,9 +84,9 @@ function fileCountPattern {
|
|||||||
file=$1
|
file=$1
|
||||||
shift
|
shift
|
||||||
string=$@
|
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 )
|
num=$(grep -ciE "${string}" $file )
|
||||||
echo -ne "$num\t"
|
echo -ne "$num;"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Ok if file contains N pattern
|
# Ok if file contains N pattern
|
||||||
@@ -98,12 +98,12 @@ function fileMustContainNItem {
|
|||||||
n=$2
|
n=$2
|
||||||
shift
|
shift
|
||||||
string=$@
|
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 )
|
num=$(grep -ciE "${string}" $file )
|
||||||
if [ $num -eq $1 ] ;
|
if [ $num -eq $1 ] ;
|
||||||
then echo -ne "1\t"
|
then echo -ne "1;"
|
||||||
else
|
else
|
||||||
echo -ne "0\t"
|
echo -ne "0;"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,30 +114,30 @@ function fileMustNOTContain {
|
|||||||
file=$1
|
file=$1
|
||||||
shift
|
shift
|
||||||
string=$@
|
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 ) ;
|
if ( grep -i -qE "${string}" $file ) ;
|
||||||
then echo -ne "0\t"
|
then echo -ne "0;"
|
||||||
else
|
else
|
||||||
echo -ne "1\t"
|
echo -ne "1;"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Ok if given user exists
|
# Ok if given user exists
|
||||||
# Arg1: user
|
# Arg1: user
|
||||||
function userExists {
|
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
|
# Ok if given group exists
|
||||||
# Arg1: group
|
# Arg1: group
|
||||||
function groupExists {
|
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
|
# insert hostname at beginning of result line
|
||||||
function addHeader {
|
function addHeader {
|
||||||
test -f /etc/motd && user="$(cat /etc/motd | sed 's/\n//')\t"
|
test -f /etc/motd && user="$(cat /etc/motd | sed 's/\n//');"
|
||||||
echo -ne "${user}$(hostname -s)\t"
|
echo -ne "${user}$(hostname -s);"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Ok if given package is installed
|
# Ok if given package is installed
|
||||||
@@ -145,7 +145,7 @@ function addHeader {
|
|||||||
function pkgInstalled {
|
function pkgInstalled {
|
||||||
pkglist=/tmp/pkg.list
|
pkglist=/tmp/pkg.list
|
||||||
test -f $pkglist || rpm -qa > $pkglist
|
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
|
# Ok if given package is NOT installed
|
||||||
@@ -153,20 +153,20 @@ function pkgInstalled {
|
|||||||
function pkgNotInstalled {
|
function pkgNotInstalled {
|
||||||
pkglist=/tmp/pkg.list
|
pkglist=/tmp/pkg.list
|
||||||
test -f $pkglist || rpm -qa > $pkglist
|
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
|
# ok if given process is running
|
||||||
# arg1: process to check
|
# arg1: process to check
|
||||||
function processIsRunning {
|
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
|
#Ok if given command returns 0
|
||||||
# arg1: command to be run
|
# arg1: command to be run
|
||||||
function commandIsWorking {
|
function commandIsWorking {
|
||||||
command=$1
|
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
|
# ok if given pattern is detected on command's stdout or stderr
|
||||||
@@ -176,21 +176,21 @@ function commandStdoutPattern {
|
|||||||
command=$1
|
command=$1
|
||||||
shift
|
shift
|
||||||
pattern=$@
|
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
|
# ok if given service is enabled
|
||||||
# arg1: service
|
# arg1: service
|
||||||
function serviceIsEnabled {
|
function serviceIsEnabled {
|
||||||
service=$1
|
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
|
# ok if given service is active
|
||||||
# arg1: service
|
# arg1: service
|
||||||
function serviceIsActive {
|
function serviceIsActive {
|
||||||
service=$1
|
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
|
# ok if output of given url contains given stuff
|
||||||
@@ -199,5 +199,5 @@ function serviceIsActive {
|
|||||||
function okIfCurl {
|
function okIfCurl {
|
||||||
url=$1
|
url=$1
|
||||||
shift
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user