shell之时间总结

在linux系统内部,日期被存储成一个整数,其取值为自1970年1月1日0时0分0秒①起所流逝的秒数。这种计时方式称为纪元时或Unix时间

$ date +%s #打印纪元时
1551947757

$  date --date "Wed mar 15 08:09:16 EDT 2017" +%s #将日期转换成纪元时
1489579756

一年中的第几天,第几周,一周中的第几天

$ date +%j #一年中的第几天
027

$ date +%d #一个月中的第几天
27
 
$ date +%w #一周中的第几天(0(周日) --> 6(周六))
3

$ date +%U #一年中的第几周
04

# 可以用--date指定日期
$ date --date "Jan 20 2001" +%A  #根据指定的日期找出这一天是星期几
Saturday

前后时间,加ago就是前,没有ago就是后

$ date -d '1 years ' "+%Y-%m-%d %H:%M:%S"
2020-03-07 17:08:35
$ date -d '1 years ago' "+%Y-%m-%d %H:%M:%S"
2018-03-07 17:07:00
$ date -d '1 months ago' "+%Y-%m-%d %H:%M:%S"
2019-02-07 17:07:08
$ date -d '1 days ago' "+%Y-%m-%d %H:%M:%S"
2019-03-06 17:07:23
$ date -d '1 hours ago' "+%Y-%m-%d %H:%M:%S"
2019-03-07 16:07:29
$ date -d '1 minutes ago' "+%Y-%m-%d %H:%M:%S"
2019-03-07 17:06:38
$ date -d '1 seconds ago' "+%Y-%m-%d %H:%M:%S"
2019-03-07 17:07:43
$ date -d '1 seconds ' "+%Y-%m-%d %H:%M:%S"
2019-03-07 17:08:05

输出固定格式的当前时间

$ date #读取日期
Thu Mar  7 16:35:40 CST 2019

$  date "+%d %B %Y" 
07 March 2019

$ date +%F
2019-03-07

$ date +%T
16:56:19

$ date +%Y%m%d%H%M%S
20190307165720

$ $ echo `date +%F | sed 's/-//g'``date +%T | sed 's/://g'`
20190307165745

在这里插入图片描述
在这里插入图片描述

计算一组命令花费的时间

date命令的最小精度是秒。

#!/bin/bash 
#文件名: time_take.sh 
start=$(date +%s) 
echo "111111111"; 
sleep 1;
echo "111111111"; 
end=$(date +%s) 
difference=$(( end - start)) 
echo Time taken to execute commands is $difference seconds.

定时器

时间粒度为秒

#!/bin/bash 
read -p "please input second:"  min 
echo Count: 
tput sc 

for count in `seq 0 ${min}`   
do 
 tput rc 
 tput ed 
 echo -n $count 
 sleep 1 
done 

tput命令

tput 可以更改终端功能,如移动或更改光标,更改文本属性,清除终端屏幕的特定区域等。

  • 光标属性

在shell脚本或命令行中,可以利用tput命令改变光标属性。

tput clear      # 清除屏幕
tput sc         # 记录当前光标位置
tput rc         # 恢复光标到最后保存位置
tput civis      # 光标不可见
tput cnorm      # 光标可见
tput cup x y    # 光标按设定坐标点移动
  • 文本属性

tput可使终端文本加粗、在文本下方添加下划线、更改背景颜色和前景颜色,以及逆转颜色方案等。

tput blink      # 文本闪烁
tput bold       # 文本加粗
tput el         # 清除到行尾
tput smso       # 启动突出模式
tput rmso       # 停止突出模式
tput smul       # 下划线模式
tput rmul       # 取消下划线模式
tput sgr0       # 恢复默认终端
tput rev        # 反相终端

此外,还可以改变文本的颜色 【在ubuntu不行】

tput setb 颜色代号   
tput setf 颜色代号

颜色代号为

0:黑色
1:蓝色
2:绿色
3:青色
4:红色
5:洋红色
6:黄色
7:白色

使用方法

RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
RESET=$(tput sgr0)
echo "${RED}red text ${GREEN}green text${RESET}"

倒计时

#!/bin/bash
read -p "please input minutes:"  min    #输入倒计时的分钟数
read -p "please input seconds:"  sec    #输入倒计时的秒数
i=min*60+sec                         #计算出总的倒计时时间
for((time=$i;time>0;time--))
do
    a=$[$time/60]
    b=$[$time%60]
    echo -ne "\r$a:$b       \r"
    sleep 1
done

模拟时钟

#!/bin/bash
# 先保存光标位置,每输出一次时间之后,就恢复光标位置、并清除光标之后的内容。

tput sc   

while true
do
        echo -ne $(date +'%Y-%m-%d %H:%M:%S') 
        sleep 1
        tput rc
        tput ed
done

# tput el; tput cnorm                            # 退出时清理终端,恢复光标显示

日历

#!/bin/bash
ch=1
while [ $ch  != "0" ]
do
echo "******************************"
echo "  0.Exit                      "
echo "  1.Display Calendar of month "
echo "  2.Display Calendar of year  "
echo "******************************"
echo "Please choose number 1,2,or,0:"
read ch
if [ $ch = "1" ]
then echo "imput year:"
     read year
     echo "input month:"
     read month
     cal $month $year
else if [ $ch = "2" ]
     then  echo "input year:"
           read year
           cal  $year
     else if [ $ch = "0" ]
           then echo "Exit now!"
           else echo "Wrong choose!"
                echo "Please choose again!"
          fi
     fi
fi
echo "   "
done

比较时间

#!/bin/bash
 
date1="2008-4-09 12:00:00"
date2="2008-4-10 15:00:00"
 
t1=`date -d "$date1" +%s`
t2=`date -d "$date2" +%s`
 
if [ $t1 -gt $t2 ]; then
    echo "$date1 > $date2"
elif [ $t1 -eq $t2 ]; then
    echo "$date1 == $date2"
else
    echo "$date1 < $date2"
fi

参考《linux shell脚本攻略》
https://www.cnblogs.com/argor/p/7911108.html
https://blog.csdn.net/qq_35958788/article/details/82861075
https://blog.csdn.net/qq_35958788/article/details/82861075
https://blog.csdn.net/Chen_dSir/article/details/53464296

猜你喜欢

转载自blog.csdn.net/zhizhengguan/article/details/88314174
今日推荐