From 47a66a3ee539c9eb68b3263987b71e1094444bc2 Mon Sep 17 00:00:00 2001 From: jimsihk <99048231+jimsihk@users.noreply.github.com> Date: Sun, 30 Jun 2024 18:27:20 +0800 Subject: [PATCH] Check value before update --- .../02-configure-moodle.sh | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/rootfs/docker-entrypoint-init.d/02-configure-moodle.sh b/rootfs/docker-entrypoint-init.d/02-configure-moodle.sh index 3d0c23a..d3cbbcc 100755 --- a/rootfs/docker-entrypoint-init.d/02-configure-moodle.sh +++ b/rootfs/docker-entrypoint-init.d/02-configure-moodle.sh @@ -12,13 +12,22 @@ cfg_file="${WEB_PATH}"/admin/cli/cfg.php # # Function to update or add a configuration value +# Usage: update_or_add_config_value update_or_add_config_value() { + # The configuration key (e.g., $CFG->wwwroot) local key - key=$(echo "$1" | sed 's|\[|\\[|g' | sed 's|\]|\\]|g' | sed 's|\/|\\/|g') # The configuration key (e.g., $CFG->wwwroot), need to escape special characters for grep and sed + key="$1" + # Need to escape special characters for grep and sed + local safekey + safekey=$(echo "$1" | sed 's|\[|\\[|g' | sed 's|\]|\\]|g' | sed 's|\/|\\/|g') + + # The new value for the configuration key local value - value="$2" # The new value for the configuration key + value="$2" + + # Avoid adding quote local noquote - noquote="$3" # Avoid adding quote + noquote="$3" if [ "$value" = 'true' ] || [ "$value" = 'false' ] || [ -n "$noquote" ]; then # Handle boolean values without quotes @@ -28,24 +37,44 @@ update_or_add_config_value() { quote="'" fi - if grep -q "$key" "$config_file"; then + if grep -q "$safekey" "$config_file"; then if [ -z "$value" ]; then # If value is empty, remove the line with the key if it exists - echo "Removed $key from config.php" - sed -i "/$key/d" "$config_file" + echo '*' "Removed $key from config.php" + sed -i "/$safekey/d" "$config_file" else - # If the key exists, replace its value - echo "Updated $key in config.php" #TODO: do not update if no change - sed -i "s|\($key\s*=\s*\)[^;]*;|\1$quote$value$quote;|g" "$config_file" + check_result="" + # Create a temporary PHP script + cat << EOF > temp_check_cfg.php + check_db_availability() { local db_host="$1" local db_port="$2"