From ce5c0c10d49311973010523106dc4fd1eb361926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D0=B5=D0=BC=D0=B8=D0=B9?= Date: Sat, 19 Jun 2021 12:17:42 +0300 Subject: [PATCH] add common.sh and test_common.sh --- 1 | 7 +++++++ common.sh | 28 ++++++++++++++++++++++++---- test_common.sh | 21 +++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 1 create mode 100644 test_common.sh diff --git a/1 b/1 new file mode 100644 index 0000000..ceafc59 --- /dev/null +++ b/1 @@ -0,0 +1,7 @@ +#!/bin/bash +lol() { + echo "$@" + echo ----- + echo "$*" +} +lol $(echo -e "1\n2\n3") diff --git a/common.sh b/common.sh index 11bbba4..092b1c9 100644 --- a/common.sh +++ b/common.sh @@ -8,13 +8,33 @@ INTERNAL_DIR="${DESTDIR}var/lib/linux-infosec-setupper" AUDIT_RULES_FILE=${DESTDIR}etc/audit/rules.d/90-linux-infosec-setupper.rules AUDIT_DAEMON_CONFIG=${DESTDIR}etc/audit/auditd.conf -_check_argument() { - case "$1" in - + +error() { + printf "$@" 1>&2 + echo '' 1>&2 +} +_check_argument_is_number() { if [[ "$1" == [0-9]* ]]; then return 0 else - printf $"Argument to %s must be a number" "$2" + error $"Argument to %s must be a number" "$2" return 1 fi } +_check_argument_value() { + if [[ "$1" < "$2" ]]; then + error $"Argument to %s must be greater than %s" "$2" "$3" + return 1 + else + return 0 + fi +} +_check_argument_is_string() { + if [[ "$1" == *[[:blank:]]* ]]; then + error $"Argument to %s must be a string without spaces" "$2" + return 1 + else + return 0 + fi +} + diff --git a/test_common.sh b/test_common.sh new file mode 100644 index 0000000..ccbe2bf --- /dev/null +++ b/test_common.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +#set -x + +TESTING=1 + +. ./common.sh + +_check_argument_is_number 123 "this" || failed="$((++failed))" +_check_argument_is_number NotNumber "this" && failed="$((++failed))" + +_check_argument_value 8 7 "this" || failed="$((++failed))" +_check_argument_value 1 7 "this" && failed="$((++failed))" + +_check_argument_is_string "Hello" "this" || failed="$((++failed))" +_check_argument_is_string "Hello world" "this" && failed="$((++failed))" + + +failed="${failed:-0}" +echo "$failed" +exit "$failed"