Files
eval_functions/functions.sh

352 lines
7.2 KiB
Bash
Executable File

#! /bin/bash
function e {
echo -ne "${1};"
}
# get machine number from name
# arg1: name of machine
function getMachineNumber {
latest=${1: -2}
left=${latest:0:1}
match='[0-9]'
if [[ "$left" =~ $match ]] ; then
echo $latest
else
echo ${1: -1}
fi
}
# file age
# return 0 if given file mtime is older than given age (in day)
# arg1: file
# arg2: age to compare
function _fileOlderThan {
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
test $(find test -mtime +$2)
return $?
}
# file mtime
# arg1: file
function fileMTime {
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
stat --printf "%y;" $1
}
# file numline
# arg1: file
function fileNumLines {
if [ ! -f $1 ] ; then echo -ne "0;" ; return 0 ; fi
n=$(wc -l $1 | awk '{print $1}')
echo -ne "$n;"
}
#ok if given program returns given code
# arg1: chroot
# arg2: program to run
# arg2: expected return code
function exitCodeOk {
chroot=$1
program=$2
code=${3}
chroot ${chroot} bash -c "$program" &> /dev/null
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;" ; return 0 ; fi
filesize=$(stat --format "%s" $1)
if [ $filesize -lt $2 ] ; then echo -ne "0;" ; else echo -ne "1;" ; fi
}
#Ok if file exists
# arg1: file to check
function fileMustExists {
if [ -f ${1} ] ; then echo -ne "1;"
else echo -ne "0;"
fi
}
#Ok if file DOESNOT exists
# arg1: file to check
function fileMustNOTExists {
if [ -f ${1} ] ; then echo -ne "0;"
else echo -ne "1;"
fi
}
#Ok if dir exists
# arg1: dir to check
function dirMustExists {
if [ -d ${1} ] ; then e 1
else e 0
fi
}
# Ok if given dir DOESNOT exists
# arg1: dir
function dirMustNOTExists {
if [ -d ${1} ] ; then echo -ne "0;"
else echo -ne "1;"
fi
}
# Ok if some files of given dir contains at least one occurence of pattern
# arg1: dir
# arg2: pattern
function dirMustContains {
dir=$1
shift
string=$@
if [ ! -d $dir ] ; then echo -ne "0;" ; return 0 ;fi
if ( grep -ri -qE "${string}" $dir ) ;
then echo -ne "1;"
else
echo -ne "0;"
fi
}
# Ok if file contains at least one occurence of pattern
# arg1: file
# arg2: pattern
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
}
# Return num of occurences of pattern
# arg1: file
# arg2: pattern
function fileCountPattern {
file=$1
shift
string=$@
if [ ! -f $file ] ; then e 0 ; return 0 ;fi
local size=$(stat --printf '%s' $file)
if [ $size -eq 0 ] ; then e 0 ; return 0 ; fi
num=$(grep -ciE "${string}" $file )
e "$num"
}
# Ok if file contains N pattern
# arg1: file
# arg2: num of expected item
# arg3: pattern
function fileMustContainsNItem {
file=$1
n=$2
shift
string=$@
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ;fi
num=$(grep -ciE "${string}" $file )
if [ $num -eq $1 ] ;
then echo -ne "1;"
else
echo -ne "0;"
fi
}
# Ok if file DOESNOT contain pattern
# Arg1: file
# ArgN: pattern
function fileMustNOTContains {
file=$1
shift
string=$@
if [ ! -f $file ] ; then echo -ne "0;" ; return 0 ; fi
if ( grep -i -qE "${string}" $file ) ;
then echo -ne "0;"
else
echo -ne "1;"
fi
}
# Ok if given user exists
# Arg1: chroot dir
# Arg2: user
function userExists {
if ( grep -iq $2 ${1}/etc/passwd ) ; then e 1 ; else e 0 ; fi
}
# Ok if given group exists
# Arg1: chroot
# Arg2: group
function groupExists {
if ( grep -iq $2 ${root}/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//');"
echo -ne "${user}$(hostname -s);"
}
# get file content
# arg1: file to read
function getFileContent {
test -f $1 && content="$(cat $1 | sed 's/\n//');"
echo -ne "${content}"
}
# get file md5
# arg1: file to checksum
function md5 {
test -f "$2" && local am=$(md5sum "$1" | awk '{print $1}')
echo -ne "${am};"
}
# cleanUppkgList
# arg1: rootfs
function cleanUppkgList {
test -d ${1} || return 0
test -f ${1}/tmp/pkg.list && rm -f ${1}/tmp/pkg.list
}
# init pkg list
# arg1: rootfs
function initPkgList {
root="$1"
test -f "${1}/tmp/pkg.list" || chroot "$1" sh -c "rpm -qa > /tmp/pkg.list"
}
# Ok if given package is installed
# arg1: rootfs
# arg2: pkg to check
# WARNING: package list might not be up to date
function pkgInstalled {
if (grep -qi "$2" "${1}/tmp/pkg.list" ) ; then e 1 ; else e 0 ; fi
}
# Ok if given package is NOT installed
# arg1: pkg to check
# WARNING: package list might not be up to date
function pkgNotInstalled {
if (grep -qi "$2" "${1}/tmp/pkg.list" ) ; then e 0 ; else e 1 ; fi
echo -ne "${?};"
}
# WIP: dont work
# ok if given process is listening on given port
# arg1: chroot
# arg2: process to check
# arg3: port
function processIsListening {
chroot=$1
process=$2
port=$3
chroot $chroot bash -c "ss -taupen|grep -q \"LISTEN.*:${port}.*${process}\""
test $? -eq 0 && e 1 || e 0
}
# WIP: dont work
# ok if given process is running
# arg1: chroot
# arg2: process to check
function processIsRunning {
chroot=$1
process=$2
chroot $chroot bash -c "ps -ef | grep -q $process"
test $? -ne 0 && e 1 || e 0
}
#Ok if given command returns 0
# arg1: chroot dir
# arg2: command to be run
function commandIsWorking {
chroot=$1
shift
command="$@"
chroot ${chroot} bash -c "$command" &> /dev/null
ret=$?
if [ $ret -eq 0 ] ; then e 1 ; else e 0 ; fi
}
# get num of files in given dir
# arg1: dir
function numFilesInDir {
local dir=$1
test -d $dir || { e 0 ; return 0 ; }
local n=$(ls -l $dir | wc -l)
e $n
}
# check wether file mode is correct
# arg1: file
# arg2: mode
function modeIsCorrect {
file=$1
mode=$2
if [ ! -e $1 ] ; then e 0 ; return 0 ; fi
current_mode=$(stat --format "%a" $1)
if [ "$mode" = "$current_mode" ] ; then e 1 ; else e 0 ; fi
}
# check wether file owner is correct
# arg1: file
# arg2: owner
function ownerIsCorrect {
file=$1
owner=$2
if [ ! -e $1 ] ; then e 0 ; return 0 ; fi
current_owner=$(stat --format "%u" $1)
if [ "$owner" = "$current_owner" ] ; then e 1 ; else e 0 ; fi
}
# check wether file group is correct
# arg1: file
# arg2: group
function groupIsCorrect {
file=$1
group=$2
if [ ! -e $1 ] ; then e 0 ; return 0 ; fi
current_group=$(stat --format "%g" $1)
if [ "$group" = "$current_group" ] ; then e 1 ; else e 0 ; fi
}
# ok if given pattern is detected on command's stdout or stderr
# arg1: chroot
# arg2: command
# arg3: pattern
function commandStdoutPattern {
local chroot=$1
local command=$2
local pattern="$3"
result=$(chroot ${chroot} bash -c "$command" 2> /dev/null)
if ( echo -n "$result" | grep -qiE "${pattern}" ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}
# ok if given service is enabled
# arg1: service
function serviceIsEnabled {
local root=$1
local service=${2}.service
c=$(ls -l ${root}/etc/systemd/system/multi-user.target.wants/ | grep -i ${service} | wc -l)
if [ $c -gt 0 ] ; then e 1 ; else e 0 ; fi
}
# ok if output of given url contains given stuff
# arg1: url
# arg2: string
function okIfCurl {
url=$1
shift
if ( curl -q --silent "${url}" | grep -qE "$@" ) ; then echo -ne "1;" ; else echo -ne "0;" ; fi
}