a13698822086 发表于 2018-8-21 12:11:11

shell的部分习题(持续更新)

#!/bin/bash  
#
  
#
  
read -p "Input your the month of yourbirthday,the format such as 05 :" Birthmonth
  
while [[ ! "$Birthmonth" =~ || $Birthmonth -lt 1 || $Birthmonth -gt 12 ]];do
  
//判断用户是否输入两位数字,个位数需用零补全
  
read -p "Input two digits,the value must between in 01 and 12,such as 04 :" Birthmonth
  
done
  
read -p "Input your the day of your birthday,the format such as 25 : " Birthday
  
until [[ $Birthday =~ && $Birthday -gt 0 && $Birthday -le 31 ]];do
  
   read -p "Input two digits,and the value must in 01 and 31,such as 25 :" Birthday
  
done
  
Year=`date +%Y`                           //定义变量,简化后续引用
  
J=`date +%s`
  
if [ $Birthmonth$Birthday -eq `date +%m%d` ];then   //if判断如果生日与现在时间相等
  
   echo -e "\033[5;31mToday is your birthday,happy birthday to you.\033[0m"
  
elif [ $Birthmonth$Birthday -gt `date +%m%d` ];then   //如果生日还未到
  
   I=`date -d $Year$Birthmonth$Birthday +%s`
  
   echo -e "\033[5;32m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday"
  
else                                 //else生日已过的情况
  
   if [ $Birthmonth$Birthday -eq 0229 ];then       //如果生日是2/29号
  
      case $((`date +%Y`%4)) in               //判断今年是否是闰年
  
      0)                              //如果今年是闰年
  
      I=`date -d $(($Year+4))$Birthmonth$Birthday +%s`
  
echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday"
  
;;
  
      1)
  
      I=`date -d $(($Year+3))$Birthmonth$Birthday +%s`
  
echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday"
  
;;
  
      2)
  
      I=`date -d $(($Year+2))$Birthmonth$Birthday +%s`
  
echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday"
  
;;
  
      3)
  
      I=`date -d $(($Year+1))$Birthmonth$Birthday +%s`
  
echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday"
  
;;
  
      esac
  
    else                              //如果生日不是2/29号
  
      I=`date -d $(($Year+1))$Birthmonth$Birthday +%s`
  
echo -e "\033[5;35m$(((($I-$J))/60/60/24+1))\033[0m days later will be your birthday"
  
    fi
  
fi


页: [1]
查看完整版本: shell的部分习题(持续更新)