我正在尝试制作一个UNIX脚本,用于计算用户输入的生日之前剩余的天数和小时数。我只能让用户输入他的生日并将其分为几个月,一年和当前的日期信息,但我认为我做的减法错误
echo -n "Enter the birthdate (mm-dd-yyyy): "
read bdate
bmonth=${bdate:0:2}
bday=${bdate:3:2}
byear=${bdate:6:4}`
`cdate='date +%m-%d-%Y'/
cmonth=${cdate:0:2}
cday=${cdate:3:2}
cyear=${cdate:6:4}
diffdays=$(( ($bdate - $cdate) / (60*60*24) ))
echo $difdays
以下是有效的。
#!/bin/bash
## Just using a YYYY-MM-DD format
echo -n "Enter the birthdate (YYYY-MM-DD):"
read bdate
## convert to epoch time
bdate=`date -d "${bdate}" +"%s"`
## convert to epoch time
current_date=`date +"%s"`
## perform a calculation divide to find the no. of days
echo $(($((bdate - current_date)) / 86400))