shell脚本计算离生日还有多少天?

1.计算测试:

注:分别输入5和1代表5月1日

注:此为计算结果


2.脚本如下:(9.11.12行-c后面的列数需要按照你本机date显示的去修改,下面参数有注释)

#!/bin/bash
#Aiden
#QQ:2575815569
#Please check the exact date of your computer
#How many days are there to calculate to the next few days.
read -p "Please enter your calculate moon (例如:6):" moon
read -p "Please enter your calculate day (例如:18):" day
clear
echo "Now the time is `date|cut -c7-13`"
declare date=`date`
nowmonth=`date|cut -c7-8`
nowday=`date|cut -c11-12`
declare -i oday=`expr $day - $nowday`
declare -i omonth=`expr $moon - $nowmonth`
declare -i cheng=`expr $omonth \* 30`
declare -i jia=`expr $cheng + $oday`
declare -i jian=`expr $nowday - $day`
declare -i jieguo=`expr $cheng - $jian`
declare -i jian2=`expr $nowmonth - $moon`
declare -i cheng2=`expr $jian2 \* 30 + $jian`
declare -i year=`expr 365 - $cheng2`
declare -i year2=`expr $year / 30`
declare -i year3=`expr $year % 30`
declare -i year4=`expr 365 - $jian`
declare -i year5=`expr $year4 / 30`
declare -i year6=`expr $year4 % 30`
if [ $moon -gt $nowmonth ]&&[ $day -gt $nowday ];then
        echo "距离$moon月$day日还有$omonth月零$oday天."
        echo "也就是$jia天."
elif [ $moon -gt $nowmonth ]&&[ $day -lt $nowday ];then
        echo "距离$moon月$day日还有$jieguo天."
              if [ $jieguo -gt 30 ];then
                    echo "也就是`expr $jieguo / 30`月零`expr $jieguo % 30`天."
              fi
elif [ $moon -lt $nowmonth ]&&[ $day -lt $nowday ];then
        echo "距离$moon月$day日还有$year2月$year3天."
        echo "也就是$year天."
elif [ $moon -lt $nowmonth ]&&[ $day -gt $nowday ];then
        echo "距离$moon月$day日还有$year2月$oday天."
        echo "也就是`expr $year2 \* 30 + $oday`天."
elif [ $moon -eq $nowmonth ]&&[ $day -eq $nowday ];then
        echo "今天就是$moon月$day日哦!"     
elif [ $moon -eq $nowmonth ]&&[ $day -gt $nowday ];then
        echo "只剩$oday天了."
elif [ $moon -eq $nowmonth ]&&[ $day -lt $nowday ];then
        echo "距离$moon月$day日还有$year5月零$year6天."
        echo "也就是$year4天."
else
        clear
        echo "输入有误!请输入纯数字."
fi

3,注释

if -eq等于 -ne不等于 -gt大于 -lt小于 -le小于等于 -ge大于等于 =相等 !=不等 -z空串  -n非空串
         if 如果                     elif 再如果                    else 否则                   |管道符                  $变量                 &&并且
expr +加 -减 \*乘 /除 %取余数
declare -i 声明( -i参数为数值) 这里表达声明后面的变量为数值
echo “” 输出到屏幕(``里可以执行命令)
read 基本读取 (-p可以跟多个变量)
cut 文本处理-取值命令
cut -c7-13 -c指定列 这里是7-13列(本文7-13列是月和日,7-8是月,11-12是日,请在你本机date查看日期修改相对列数)

有bug和意见可以直接评论或者私信我都可以


不支持中文字符的同学请自行把汉子和!修改掉哦

猜你喜欢

转载自blog.csdn.net/loame_zyq/article/details/80321487
今日推荐