Voici votre URL de partage https://sharemycode.io/c/8d79b90 (Cliquer pour copier) (Copié)

Nom du fichier : scratch.sh

#!/usr/bin/env bash

PREVIOUS_OUTPUT_DIR="/tmp/previous_output"
EXPECTED_SWAPPINESS=5
DEBUG=false

mkdir -p "$PREVIOUS_OUTPUT_DIR"

declare -A conditions
conditions=(
  ["/etc/ssl/*.*"]="find /etc/ssl/*.* -type f -exec md5sum {} + 2>/dev/null"
  ["/opt/zing tree"]="find /opt/zing -type f -printf '%y %M %TD %TT %p\n' 2>/dev/null | sort"
  ["$HOME/.cert"]="find $HOME/.cert -type f -exec md5sum {} + 2>/dev/null"
  ["$HOME/.krb"]="find $HOME/.krb -type f -exec md5sum {} + 2>/dev/null"
  ["/etc/zing/pmem.conf.*"]="find /etc/zing/pmem.conf.* -type f -exec md5sum {} + 2>/dev/null"
  ["/proc/cmdline"]="cat /proc/cmdline 2>/dev/null"
  ["Swappiness"]="current_swappiness=\$(cat /proc/sys/vm/swappiness 2>/dev/null); if [ \"\$current_swappiness\" -ne \"$EXPECTED_SWAPPINESS\" ]; then echo 'Swappiness is different from expected ($EXPECTED_SWAPPINESS). Current value: \$current_swappiness'; else echo 'Swappiness is as expected ($EXPECTED_SWAPPINESS)'; fi"
)

declare -A statuses
statuses=(
  ["changed"]="Changed"
  ["not_changed"]="Not changed"
  ["error"]="Error"
  ["new"]="New"
)

log_debug() {
  [ "$DEBUG" = true ] && echo "[$(date)] $*"
}

capture_output() {
  local command="$1"
  local output
  if ! output=$(eval "$command" 2>/dev/null); then
    log_debug "Error capturing output for command: $command" >&2
    return 1
  fi
  echo "$output"
}

calculate_checksum() {
  local output="$1"
  echo -n "$output" | md5sum | cut -d' ' -f1
}

store_output_and_checksum() {
  local condition="$1"
  local output="$2"
  local output_file="$3"

  local checksum
  checksum=$(calculate_checksum "$output")

  {
    echo "Checksum: $checksum"
    echo "Output:"
    echo "$output"
  } > "$output_file"

  log_debug "$condition checksum: $checksum stored in $output_file"
}

load_checksum() {
  local file="$1"
  grep '^Checksum: ' "$file" | cut -d' ' -f2
}

compare_checksums() {
  local current_output="$1"
  local previous_output_file="$2"

  if [ -f "$previous_output_file" ]; then
    local previous_checksum
    local current_checksum
    previous_checksum=$(load_checksum "$previous_output_file")
    current_checksum=$(calculate_checksum "$current_output")
    [ "$current_checksum" != "$previous_checksum" ]
    return 0  # Checksums differ
  fi
  return 1  # Previous output file does not exist (new condition)
}

clear_state() {
  DEBUG=true
  log_debug "Clearing environmental files..."
  rm -f "$PREVIOUS_OUTPUT_DIR"/*
  log_debug "All created files have been cleared."
}

cleanup() {
  log_debug "Detection of environment changes finished"
}
trap cleanup EXIT

main() {
  log_debug "Detection of environment changes started"

  echo "Condition,Status"
  for condition in "${!conditions[@]}"; do
    command="${conditions[$condition]}"
    previous_output_file="$PREVIOUS_OUTPUT_DIR/$(calculate_checksum "$condition").txt"

    current_output=$(capture_output "$command")
    if [ -z "$current_output" ]; then
      echo "$condition,${statuses[error]}"
      continue
    fi

    if [ ! -f "$previous_output_file" ]; then
      store_output_and_checksum "$condition" "$current_output" "$previous_output_file"
      echo "$condition,${statuses[new]}"
    else
      status="${statuses[not_changed]}"
      if compare_checksums "$current_output" "$previous_output_file"; then
        store_output_and_checksum "$condition" "$current_output" "$previous_output_file"
        status="${statuses[changed]}"
      fi
      echo "$condition,$status"
    fi
  done
}

if [ "$1" = "--debug" ]; then
  DEBUG=true
elif [ "$1" = "--clear" ]; then
  clear_state
  exit 0
fi

main

Informations

Cet extrait a été créé le 24 juin 2024 à 16:26:27

Cet extrait expire le 24 juil. 2024 à 16:26:27

Langage : html

Logo html

Link

Voici votre URL de partage : https://sharemycode.io/c/8d79b90 Copié

Ce code a été partagé avec Share on ShareMyCode.io pour Jetbrains