Shell script does not use Data functions to implement a date query

The shell script realizes the query N days before or N days after the specified date

 1 
 1 span> #!/bin/bash

2 func(){
3 read -p "Please enter the year, month and day (format: 2019-01-01): " date
4 if echo $date |grep -Eq < span style="color: #800000;">"
[0-9]{4}-[0-9]{1,2}-[0 -9]{1,2}" && date -d $date +%Y%m%d >/dev/null 2>&1< span style="color: #000000;"> #Judging whether the format of the entered year, month, and day is correct
5 then
6 year=${date:0:< span style="color: #800080;">4}
7 month=${date:5:< span style="color: #800080;">2}
8 day=${date:8:< span style="color: #800080;">2}
9 read -p "Please enter 1 (find forward) or 2 (find backward):" num
10 #Judging whether the input is correct
11 if [[ "$num" =~ ^[ 1,2]{1 }+$ ]];then
12 calculate #if
13 else
14 echo "Please enter as required!"
15 func
16 fi
17 else
18 echo 'Please enter the date in the correct format!
19 func
20 fi
21 }
22 #Get the number of days corresponding to the month
23 getDay(){
24 case $2 in
25 1|3|5|7|8:|10|12) day= 31;;
26 4|6|9|11) day=< span style="color: #800080;">30;;
27 2)
28 if [`expr $1% 4` -eq 0 -a `expr $1% 100` -ne 0 ]; then
29 day=29
30 elif [`expr $1% 400` -eq 0 ]; then
31 day=29
32 else
33 day=28
34 fi
35 ;;
36 esac
37 return $day
38 }
39 calculate(){
40 #Find forward
41 if [$num -eq 1 ];then
42 read -p "Please enter the number of days you want to look forward:" fnum
43 if [[ "$fnum" =~ ^[ 0-9]+$ ]];then #Judging whether the input is correct
44 N=`expr ${date:8 :2} - $fnum`
45 flag=1
46 while true
47 do
48 #If the month is one month and the date to be searched needs to span the month
49 if [$month -eq 1 -a $N -le 0] ;then
50 year=`expr $year-1< span style="color: #000000;">`
51 month=12
52 N=`expr $N + 31`
53 fi
54 #If the month is not one month and the date to be searched needs to cross month
55 if [$month -ne 1 -a $N -le 0] ;then
56 month=`expr $month-1< span style="color: #000000;">`
57 getDay $year $month
58 N=`expr $N + $?`
59 fi
60 #When the number of days to look forward is less than the number of days in the month Exit the loop
61 if [$N -gt 0]
62 then
63 day=$N
64 break
65 fi
66 done
67 #Print out the results
68 echo "The date you entered is:"$date
69 echo "The date you are looking for is: $year-$month-$day"
70 else
71 echo "Please enter a pure number to find the number of days!"
72 func
73 fi
74 fi
75 #Find backward
76 if [$num -eq 2 ];then
77 read -p "Please enter the number of days you want to search backward:" bnum
78 if [[ "$bnum" =~ ^[ 0-9]+$ ]];then #Judging whether the input is correct
79 N=`expr ${date:8 :2} + $bnum`
80 flag=1
81 while true
82 do
83 month=`expr $month + 0`
84 getDay $year $month
85 maxMonth=$?
86 #When the month is December and the searched date needs to cross month
87 if [$N -gt $maxMonth- a $month -eq 12 ];then
88 year=`expr $year + 1< span style="color: #000000;">`
89 month=1
90 N=`expr $N - $maxMonth`
91 fi
92 #When the month is not 12 and the searched date needs to cross month
93 if [$N -gt $maxMonth- a $month -ne 12 ];then
94 month=`expr $month + 1< span style="color: #000000;">`
95 N=`expr $N - $maxMonth`
96 fi
97 getDay $year $month
98 #Exit when the number of days to look back will not span months cycle
99 if [$N -le $?< span style="color: #000000;"> ];then
100 day=$N
101 break
102 fi
103 done
104 #Print out the results
105 echo "The date you entered is:"$date
106 echo "The date you are looking for is: $year-$month-$day"
107 else
108 echo "Please enter a pure number to find the number of days!"
109 func
110 fi
111 fi
112
113 }
114 func

Beginning to learn Linux, the code is not standardized, please give us some advice!

 1 #!/bin/bash

2 func(){
3 read -p "Please enter the year, month and day (format: 2019-01-01): " date
4 if echo $date |grep -Eq < span style="color: #800000;">"[0-9]{4}-[0-9]{1,2}-[0 -9]{1,2}" && date -d $date +%Y%m%d >/dev/null 2>&1< span style="color: #000000;"> #Judging whether the format of the entered year, month, and day is correct
5 then
6 year=${date:0:< span style="color: #800080;">4}
7 month=${date:5:< span style="color: #800080;">2}
8 day=${date:8:< span style="color: #800080;">2}
9 read -p "Please enter 1 (find forward) or 2 (find backward):" num
10 #Judging whether the input is correct
11 if [[ "$num" =~ ^[ 1,2]{1 }+$ ]];then
12 calculate #if
13 else
14 echo "Please enter as required!"
15 func
16 fi
17 else
18 echo 'Please enter the date in the correct format!
19 func
20 fi
21 }
22 #Get the number of days corresponding to the month
23 getDay(){
24 case $2 in
25 1|3|5|7|8:|10|12) day= 31;;
26 4|6|9|11) day=< span style="color: #800080;">30;;
27 2)
28 if [`expr $1% 4` -eq 0 -a `expr $1% 100` -ne 0 ]; then
29 day=29
30 elif [`expr $1% 400` -eq 0 ]; then
31 day=29
32 else
33 day=28
34 fi
35 ;;
36 esac
37 return $day
38 }
39 calculate(){
40 #Find forward
41 if [$num -eq 1 ];then
42 read -p "Please enter the number of days you want to look forward:" fnum
43 if [[ "$fnum" =~ ^[ 0-9]+$ ]];then #Judging whether the input is correct
44 N=`expr ${date:8 :2} - $fnum`
45 flag=1
46 while true
47 do
48 #If the month is one month and the date to be searched needs to span the month
49 if [$month -eq 1 -a $N -le 0] ;then
50 year=`expr $year-1< span style="color: #000000;">`
51 month=12
52 N=`expr $N + 31`
53 fi
54 #If the month is not one month and the date to be searched needs to cross month
55 if [$month -ne 1 -a $N -le 0] ;then
56 month=`expr $month-1< span style="color: #000000;">`
57 getDay $year $month
58 N=`expr $N + $?`
59 fi
60 #When the number of days to look forward is less than the number of days in the month Exit the loop
61 if [$N -gt 0]
62 then
63 day=$N
64 break
65 fi
66 done
67 #Print out the results
68 echo "The date you entered is:"$date
69 echo "The date you are looking for is: $year-$month-$day"
70 else
71 echo "Please enter a pure number to find the number of days!"
72 func
73 fi
74 fi
75 #Find backward
76 if [$num -eq 2 ];then
77 read -p "Please enter the number of days you want to search backward:" bnum
78 if [[ "$bnum" =~ ^[ 0-9]+$ ]];then #Judging whether the input is correct
79 N=`expr ${date:8 :2} + $bnum`
80 flag=1
81 while true
82 do
83 month=`expr $month + 0`
84 getDay $year $month
85 maxMonth=$?
86 #当月份为12月并且查找的日期需要跨月
87 if [ $N -gt $maxMonth -a $month -eq 12 ];then
88 year=`expr $year + 1`
89 month=1
90 N=`expr $N - $maxMonth`
91 fi
92 #当月份不是12并且查找的日期需要跨月
93 if [ $N -gt $maxMonth -a $month -ne 12 ];then
94 month=`expr $month + 1`
95 N=`expr $N - $maxMonth`
96 fi
97 getDay $year $month
98 #当向后查找的天数不会跨月时退出循环
99 if [ $N -le $? ];then
100 day=$N
101 break
102 fi
103 done
104 #打印出结果
105 echo "您输入的日期为:"$date
106 echo "您要查找的日期为:$year-$month-$day"
107 else
108 echo "查找的天数请输入纯数字!"
109 func
110 fi
111 fi
112
113 }
114 func

Leave a Comment

Your email address will not be published.