任务:
run.sh
需要传入一个8位日期的参数,判断是否合法,然后每一秒打印当前日期加1,合计打印5分钟后退出,例如:输入20200801,屏幕每秒打印20200802,20200803 ...
stop.sh
执行该脚本,run.sh不再打印,直接退出
run.sh
while :
do
# 读取数据
read -p "输入:" datastring
# 要判断长度
if [ "$datastring" = "" ]
then
echo "输入不合法"
continue
fi
expr ${datastring} + 1 &>/dev/null
if [ $? -eq 1 ]
then
echo "输入不合法"
continue
fi
# 判断长度为8
len=`expr length ${datastring}`
if [ $len -ne 8 ]
then
echo "输入不合法"
continue
else
flag_y=0
year=${datastring:0:4}
month=${datastring:4:2}
day=${datastring:6:2}
if [ $(($month)) -gt 12 ] || [ $(($month)) -le 0 ]; then
echo "月份输入不合法"
continue
elif [ $(($day)) -gt 32 ] || [ $(($day)) -le 0 ]; then
echo "日期输入不合法"
continue
fi
# 判断是不是闰年
if [ $(($year%4)) -eq 0 ] &&[ $(($year%100)) -ne 0 ]||[ $(($year%400)) -eq 0 ]; then
flag_y=1
fi
if [ $flag_y = 1 ] && [ $(($month)) -eq 2 ] && [ $(($day)) -gt 29 ]; then
echo "日期输入不合法"
continue
elif [ $flag_y = 0 ] && [ $(($month)) -eq 2 ] && [ $(($day)) -gt 28 ]; then
echo "日期不合法"
continue
elif [ $(($month)) -eq 4 ] ||[ $(($month)) -eq 6 ] || [ $(($month)) -eq 11 ] && [ $(($day)) -gt 30 ]; then
echo "日期不合法"
continue
else
echo "日期合法"
t=1
echo $datastring
while [ $t -lt 600 ]
do
sleep 1
v_date_future_1=`date -d "$datastring 1 day" +%Y%m%d`
echo $v_date_future_1
datastring=$v_date_future_1
let t++
done
fi
fi
done
stop.sh
ps -ef | grep run.sh | grep -v grep | awk '{print $2}' | xargs kill
修改后:
run.sh
while :
do
read -p "输入:" n1
# 判断为空
if [ "$n1" = "" ]; then
echo "输入不合法"
continue
fi
n1=`echo $n1`
# 判断长度为8
len=`expr length ${n1}`
if [ $len -ne 8 ]; then
echo "输入不合法"
continue
else
n1=`date -d "$n1" +%Y%m%d`
if [ $? -eq 0 ]; then
t=1
echo $n1
while [ $t -lt 300 ]
do
`ps -ef | grep stop.sh | grep -v grep` &>/dev/null
if [ $? -eq 1 ]; then
sleep 1
v_date_future_1=`date -d "$n1 1 day" +%Y%m%d`
echo $v_date_future_1
n1=$v_date_future_1
let t++
else
exit 1
fi
done
fi
fi
done
stop.sh
sleep 2