我只是想知道这个脚本最后我做错了什么,没有将我的检查日期放入 cal 命令中

我只是想知道这个脚本最后我做错了什么,没有将我的检查日期放入 cal 命令中
#Purpose: Validate a date
#Usage: chkdate year month day
#
if [ $# -eq 0 ]; then
        echo Usage: chkdate year month day
        exit 0
fi
year=$1; month=$2; day=$3; extra=$4
if [[ "$extra" != "" ]]; then
        # Too much data!
        echo Usage: chkdate year month day
        exit 1
fi
if [[ "$year" == "" || "$month" == "" || "$day" == "" ]]; then
        # Not enough data!
        echo Usage: chkdate year month day
        exit 2
fi
if [[ ! ( $year =~ ^[0-9]+$ && $month =~ ^[0-9]+$ && $day =~ ^[0-9]+$ ) ]]; then
        # Date not numeric!
        echo Usage: chkdate year month day
        exit 3
fi
#Remove leading zeros if any
year=$(echo "$year" | bc)
month=$(echo "$month" | bc)
day=$(echo "$day" | bc)
if [[ $year -lt 1 || $year -gt 9999 || $month -lt 1 || $month -gt 12 || $day -lt 1 || $day -gt 31 ]]; then
        # Date out of range!
        echo Usage: chkdate year month day
        exit 4
fi
if [[ ( $month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12 ) && $day -gt 31 ]]; then
        # Invalid day!
        echo Usage: chkdate year month day
        exit 5
fi
if [[ ( $month == 4 || $month == 6 || $month == 9 || $month == 11 ) && $day -gt 30 ]]; then
        # Invalid day!
        echo Usage: chkdate year month day
        exit 6
fi
if [[ $month == 2 && $(($year%100)) == 0 ]]; then
        if [[ $(($year%400)) == 0 ]]; then
                isLeapYear=1
        else
                isNotLeapYear=1
        fi
elif [[ $month == 2 && $(($year%4)) == 0 ]]; then
        isLeapYear=1
else
        isNotLeapYear=1
fi
if [[ $month == 2 && $day -gt 28 && $isNotLeapYear ]]; then
        # Not Leap year!
        echo Usage: chkdate year month day
        exit 7
fi
if [[ $month == 2 && $day -gt 29 && $isLeapYear ]]; then
        # Invalid day!
        echo Usage: chkdate year month day
        exit 8
fi
while [[ $# -ne [1-8] ]]; do
cal << ( $year $month $day )
fi
echo $year $month $day is valid date

答案1

  • 添加 shebang,因为您正在使用的[[很可能#!/bin/bash就是您想要的
  • 尝试坚持使用一种测试语法(单括号[或双括号[[
  • 而不是<<(此处文档)最有可能是您想要的<<<(此处字符串)
  • 脚本循环结束时while未关闭,期待done某处
  • 有太多了fi。 (代替done???)
  • 以某种方式退出循环是个好主意......

相关内容