#!/bin/bash
echo "time now is:"
read hours
if [ $hours -lt 12 ]
then
echo "good morning"
elif [ $hours -ge 12 ] && [ $hours -lt 20 ]
then
echo "good afternoon"
else
echo "good night"
fi
调用系统时后:
#!/bin/bash
echo "time now is:"
echo `date`
#read hours
hours=`date +%H`
echo "hours $hours"
if [ $hours -lt 12 ]
then
echo "good morning"
elif [ $hours -ge 12 ] && [ $hours -lt 20 ]
then
echo "good afternoon"
else
echo "good night"
fi
这里注意的地方是
等号两边不要空格,要紧贴着。
获取系统时间之当前小时的语句是
hours=`date +%H`
如果想系统时间按格式显示,则可改为
echo `date +"%Y-%m-%d-%H:%M:%S"`
运行结果为:
这里需要注意的是:不同的大小写代表的是不同的含义
- date +%Y 以四位数字格式打印年份 eg: 2018
- date +%y 以二位数字格式打印年份 eg: 18
- date +%m 月份
- date +%d 日期
- date +%H 小时
- date +%M 分钟
- date +%S 秒
- date +%w 星期,如果结果显示0,则表示周日
此外,if语句的格式一定要正确,否则会报错:
if语句格式一
´if 条件表达式1
´then
´ 命令1
´elif 条件表达式2
´then
´ 命令2
´……
´else
´ 命令n
fi
if语句格式二
´if 条件表达式
´then
´命令1
´[else
´ 命令2]
fi