#!/bin/bash set -e set -o pipefail never_drop=( information_schema mysql performance_schema sys ) never_drop() { grep -vF ${never_drop[@]/#/ -e } } choose_drops() { mariadb -ss -b <<< 'show databases' | never_drop } execute_drops() { while read dbname do t=\` printf 'drop database %s ;\n' \ "$t${dbname//$t/$t$t}$t" done | if [ "$REALLY_DROP_ALL_MARIADB_DATABASES" ] then mariadb -v else cat fi } warn() { printf 'Warning: %s\n' "$*" >&2 } perr() { printf 'Error: %s: %s\n' "$0" "$*" >&2 } case "$#$1" in 1--drop-all-databases ) shift REALLY_DROP_ALL_MARIADB_DATABASES=y ;; 0 ) warn 'No databases will be dropped' warn 'To execute the SQL, supply' \ 'command-line argument: --drop-all-databases' ;; * ) perr "Unexpected argument list: ${@@Q}" exit 1 ;; esac choose_drops | execute_drops