歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> linuxshell實現動態時鐘

linuxshell實現動態時鐘

日期:2017/3/1 13:39:01   编辑:SHELL編程

最近再看有關linux shell方面的東西,偶爾看到說在終端上實現動態時鐘,就在網上搜了一下

http://blog.csdn.net/reage11/article/details/8586200 這個博客寫的可以 容易懂,但我再仔細看他的代碼時,發現不需要用switch來判斷月份,Linux命令的偉大之處已經超出了我們的想象,因此,我將代碼修改如下:

#!/bin/bash
tput civis
while [ 1 ]
do
   tput clear
   tput cup 3 10
   tput setb 0
   tput setf 2
   echo $(date "+%Y--%m--%d %H:%M:%S  %A")
   sleep 1

done 

原文如下:

    #!/bin/bash    
    tput civis  
    while [ 1 ]  
    do  
        nonth=$(date +%B)  
        case "$nonth" in  
            January)  nonth=1;;  
            February)  nonth=2;;  
            March)  nonth=3;;  
            April)  nonth=4;;  
            May)  nonth=5;;  
            June)  nonth=6;;  
            July)  nonth=7;;  
            August)  nonth=8;;  
            September)  nonth=9;;  
            October)  nonth=10;;  
            November)  nonth=11;;  
            December)  nonth=12;;  
        esac  
            tput clear  
            tput cup 3 10  
        echo $(date +%Y)--$nonth--$(date +%d) $(date +%H):$(date +%M):$(date +%S) $(date +%A)  
        sleep 1  
           
    done  
相關知識(從上面鏈接拷過來的):

tput命令參數介紹:

tput civis :用來隱藏光標

tput cols :顯示當前所在的列

tput lines :顯示當前所在的行

tput cup lines cols : 將光標移動到第lines行,第cols列

tput setb no :設置終端背景色。 no的取值稍後介紹

tput setf no : 設置文本的顏色。no的取值稍後介紹

tput ed :刪除當前光標到行尾的內容

no的取值:0:黑色、1:藍色、2:綠色、3:青色、4:紅色、5:洋紅色、6:黃色、7:白色

更多內容請使用 man tput 查看

date命令參數介紹

%H :小時(0..23)%I : 小時(01..12) %M : 分鐘(0..59) %p : 顯示本地時段“上午”或 “下午” %r : 直接顯示時間 (12 小時制,格式為 hh:mm:ss [AP]M) %s : 從 1970 年 1 月 1 日 00:00:00 UTC 到目前為止的秒數 %S : 秒(00..61) %X : 相當於 %H:%M:%S %a : 星期幾 (Mon..Sun) %A : 星期幾 (Monday..Sunday) %b : 月份 (Jan..Dec) %B : 月份 (January..December) %c : 直接顯示日期與時間 %d : 日 (01..31) %D : 直接顯示日期 (mm/dd/yy) %j : 一年中的第幾天 (001..366) %m : 月份 (01..12) %x : 直接顯示日期 (mm/dd/yy) %y : 年份的最後兩位數字 (00.99) %Y : 完整年份 (0000..9999)
Copyright © Linux教程網 All Rights Reserved