Linux Shell Script Raiders (1.10)

Source: Internet
Author: User
Tags shebang

1.10 Get, set date and time delay
    • Many applications need to print the date in different formats, set the date and time, and perform an action based on the date and time. Latency is typically used to provide a wait time (for example, 1 seconds) during program execution. Similarly, we can print the date in more than one format, or set the date on the command line. In a Unix-like system, a date is stored as an integer whose size is the number of seconds elapsed since the world standard Time . This timing method is called Era or Unix time .
1.10.1 Get, set time
    • The following programs give a variety of uses:
#!/bin/bashstart=$ (Date+%s)#获取纪元时间Date                                           #输出系统时间Date+%s#打印纪元时间Date--Date "Thu 08:07:21 IST"+%s#转换成纪元时间Date--Date "2001"+%a#获取当前星期几#date "+%d%B%Y" #按格式打印日期Date-S"June 11:01:22"                #设置日期和时间End=$(Date+%s)#获取纪元时间difference=$ ((End-start))#获取执行时间Echo Time Taken toExecute commands is$difference seconds.
    • Note: You can use time to get the script to execute.
1.10.2 Setting Delay
    • When you write a monitoring script that runs in a circular fashion, setting the time interval is essential. To defer execution in the script for a period of time, you can use sleep no_of_seconds.
#!/bin/bashEcho-N Count:tput SC#存储光标位置Count=0 while true; Do        if[$count -lt  +] Then             Letcount++ Sleep1;#休眠1秒钟Tput RC#恢复光标位置Tput Ed#清除当前位置到末尾的内容            Echo-N$count;Else Exit 0;fi     Done
    • The above script shows how to use time-lapse. Tput SC stores the cursor position, SC is the abbreviation for Save cursor. In each loop, a new count value is printed in the terminal by restoring the location of the previously stored cursor. The Tput RC is used to restore the cursor position, and RC is the abbreviation for the recovery cursor. Tput Ed clears everything from the current cursor position to the end of the line, so that the old value is cleared and the new value is written, and Ed can be seen as an abbreviation for erase to end. In this procedure, a 1-second delay is provided via sleep 1.
1.10.3 time settings, common parameters for reading
Date Content Format Description
Week %a (ex: Sat)
%A (ex: Saturday)
Month %b (ex: Nov)
%B (ex: November)
Day %d (for example: 31)
Fixed format date (MM/DD/YY) %d (Example: 10/18/10)
Years %y (ex: 10)
%Y (ex: 2010)
Hours %i or%h (ex: 08)
Minutes %M (ex: 33)
Seconds %s (for example: 10)
Na-Sec %N (ex: 692508515)
Unix era (in seconds) %s (for example: 1290049486)
1.10.4 Reference
    • Linux Shell Script Raiders

Linux Shell Script Raiders (1.10)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.