#!/bin/bash
get_max_days() {
local year=$1
local month=$2
local max_days
echo "进入get_max_days()"
case $month in
1|3|5|7|8|10|12)
max_days=31
;;
4|6|9|11)
max_days=30
;;
2)
if [ $((year % 4)) -eq 0 ] && ([ $((year % 100)) -ne 0 ] || [ $((year % 400)) -eq 0 ]); then
max_days=29
else
max_days=28
fi
;;
*)
echo "Error: Invalid month"
exit 1
;;
esac
echo $max_days
}
get_previous_month_date() {
local input_date=$1
if ! [[ $input_date =~ ^[0-9]{8}$ ]]; then
echo "Error: Date must be in format YYYYMMDD"
exit 1
fi
local year=${input_date:0:4}
local month=${input_date:4:2}
local day=${input_date:6:2}
year="${year#0}"
month="${month#0}"
day="${day#0}"
local current_max_days=$(get_max_days $year $month)
echo "当前月份的最大天数:$current_max_days"
if [ $? -ne 0 ]; then
exit 1
fi
month=$((month - 1))
if [ $month -eq 0 ]; then
month=12
year=$((year - 1))
fi
local max_days=$(get_max_days $year $month)
echo "上个月的最大天数:$max_days"
if [ $? -ne 0 ]; then
exit 1
fi
echo "day:$day"
echo "current_max_days:$current_max_days"
if [ $day -eq $current_max_days ]; then
day=$max_days
fi
printf "%04d%02d%02d\n" $year $month $day
}
if [ "$#" -ne 2 ]; then
echo "Usage: $0 YYYYMMDD <base_path>"
exit 1
fi
previous_date=$(get_previous_month_date $1)
if [ -z "$previous_date" ]; then
echo "Error: The calculated previous date is empty."
exit 1
fi
base_path=$2
final_path="$base_path/$previous_date"
echo $final_path
if [ -d "$final_path" ]; then
echo "目录 '$final_path' 存在。"
else
echo "目录 '$final_path' 不存在。"
fi