BASH Countdown bis Weihnachten

Ein BASH Skript, welches einen Countdown bis Weihnachten anzeigt. Kann natürlich auch für andere Tage benutzt werden.

#!/bin/bash

# Set the target date (Christmas)
target_month=12
target_day=25

# Get the current date
current_month=$(date +%m)
current_day=$(date +%d)

# Calculate the number of days until Christmas
if [ $current_month -gt $target_month ] || [ $current_month -eq $target_month -a $current_day -ge $target_day ]
then
  # Christmas has already passed this year, so set the target date to next year
  target_year=$(date +%Y)
  let "target_year+=1"
else
  # Christmas has not passed yet this year
  target_year=$(date +%Y)
fi

# Calculate the number of seconds until Christmas
target_date="$target_year-$target_month-$target_day 00:00:00"
seconds_until_christmas=$(date -d "$target_date" +%s)
current_time=$(date +%s)
let "seconds_until_christmas-=current_time"

# Calculate the number of days, hours, minutes, and seconds until Christmas
days=$((seconds_until_christmas / 86400))
hours=$((seconds_until_christmas % 86400 / 3600))
minutes=$((seconds_until_christmas % 3600 / 60))
seconds=$((seconds_until_christmas % 60))

# Print the countdown message
echo "There are $days days, $hours hours, $minutes minutes, and $seconds seconds until Christmas!"

Das Skript setzt das Ziel-Datum (Weihnachten) auf den 25. Dezember und berechnet dann die Anzahl der Tage, Stunden, Minuten und Sekunden bis zu diesem Datum. Wenn das aktuelle Datum nach dem Ziel-Datum liegt, wird das Ziel-Jahr um 1 erhöht, um den Countdown für das nächste Jahr zu berechnen.

Um das Skript auszuführen, speichere es zunächst in einer Datei mit einem beliebigen Namen und fügen Sie ihm dann Ausführungsberechtigungen hinzu:

chmod +x countdown.sh

Danach kannst du das Skript ausführen

./countdown.sh
Die mobile Version verlassen