Код: Выделить всё
#! /bin/bash
source ./scripts/_functions.bash
server_reachable=$(is_db_server_reachable)
db_exists=$(does_db_exist)
db_empty=$(is_db_empty)
public_tables=$(count_public_tables_in_db)
echo '------------------------------'
echo "Server $db_uri reachable: $server_reachable"
echo "DB $db_name exists: $db_exists"
echo "DB $db_name empty: $db_empty"
echo "DB $db_name public tables: $public_tables"
echo '------------------------------'
Код: Выделить всё
function echoerr {
# Output to stderr while freeing stdout for returns
echo -ne $red
echo -e "ERROR: $@" | ts '[%Y-%m-%d %H:%M:%S]' 1>&2
echo -ne $nc
}
function is_db_empty {
# A DB exists and empty? => true
local the_db_exists=$(does_db_exist)
if [[ "$the_db_exists" = "false" ]]; then
# NOTE: a non-existent DB cannot be empty -> return null
echoerr 'DB not found at all'
echo
else
local public_tables=$(count_public_tables_in_db)
if [[ "$public_tables" -eq '0' ]]; then
echo true
else
echoerr 'Found tables in public schema'
echo false
fi
fi
}
Код: Выделить всё
initial-tests.bash
Код: Выделить всё
#! /bin/bash
nc='\e[0m'
yellow='\e[1;33m'
green='\e[1;32m'
red='\e[1;31m'
blue='\e[1;34m'
# Is the script sourced in the same ssh process, or called in a child process?
(return 0 2>/dev/null) && sourced=1 || sourced=0
if [[ "$sourced" = '1' ]]; then
echo -e "${red}You sourced the main script. Call it in a child process using bash, otherwise you might lose the ssh session on error!${nc}"
return 10
fi
# Fail early
echo -e "${blue}- - - Initial Checks - - -${nc}"
source ./scripts/initial-tests.bash
Код: Выделить всё
[2024-10-09 11:37:33] ERROR: DB devops_testing on 127.0.0.1 not found
[2024-10-09 11:37:33] ERROR: DB devops_testing on 127.0.0.1 not found
[2024-10-09 11:37:33] ERROR: DB not found at all
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: database "devops_testing" does not exist
[2024-10-09 11:37:33] ERROR: DB devops_testing on 127.0.0.1 not found
------------------------------
Server 127.0.0.1 reachable: true
DB devops_testing exists: false
DB devops_testing empty:
DB devops_testing public tables: -1
------------------------------
Код: Выделить всё
[2024-10-09 11:37:26] ERROR: DB devops_testing on 127.0.0.1 not found
[2024-10-09 11:37:26] ERROR: DB devops_testing on 127.0.0.1 not found
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: database "devops_testing" does not exist
[2024-10-09 11:37:27] ERROR: Found tables in public schema
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: database "devops_testing" does not exist
[2024-10-09 11:37:27] ERROR: DB devops_testing on 127.0.0.1 not found
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: database "devops_testing" does not exist
./scripts/_functions.bash: line 76: ((: == 10 : syntax error: operand expected (error token is "== 10 ")
[2024-10-09 11:37:27] ERROR: Only out of 10 core tables found!
------------------------------
Server 127.0.0.1 reachable: true
DB devops_testing exists: false
DB devops_testing empty: false
DB devops_testing public tables: -1
------------------------------
Я новичок в bash. Я не знаю, почему source правильно запускает тесты в текущей оболочке, но если те же тесты получены из «разветвленного» скрипта, они ведут себя по-другому.
Подробнее информация:
- ОС: Ubuntu 24.04.1 LTS (GNU/Linux 6.8.0-1015-azure x86_64)
- Bash: GNU bash, версия 5.2.21(1)-релиз (x86_64-pc-linux-gnu)
Подробнее здесь: https://stackoverflow.com/questions/790 ... -vs-forked