Kā strādāt ar datumu un laiku Bash, izmantojot komandu Datums


Datuma komanda ir ārēja bash programma, kas ļauj iestatīt vai parādīt sistēmas datumu un laiku. Tas nodrošina arī vairākas formatēšanas iespējas. Datuma komanda pēc noklusējuma ir instalēta visos Linux distros.

$ which date
$ type -a date

Ierakstiet komandā datumu komandā, kas parādīs pašreizējo datumu un laiku.

$ date

Izmantojot komandu date, var mainīt sistēmas datumu, laiku un laika joslu, un izmaiņas ir sinhronizētas ar aparatūras pulksteni.

$ date --set="Thu Nov 12 13:06:59 IST 2020"
$ hwclock --systohc

Laba vieta, kur iegūt formatēšanas opciju sarakstu, būs cilvēka lapa.

$ man date

Apskatīsim dažas no visbiežāk izmantotajām formatēšanas opcijām.

  • Lai lietotu formatējumu, izmantojiet “+” un pēc tam “formatēt”.
  • Lai iegūtu GNU\LINUX formatēšanas opciju sarakstu, apskatiet saistīto cilvēka lapu.
  • Lai iegūtu BSD formatēšanas opciju sarakstu, apskatiet saistīto cilvēka lapu.

Divas svarīgās komandas datuma daļas izmanto opciju Formatēt +% un –date.

Tagad komandai datums jāpielieto daži formatējumi. Lai lietotu formatējumu, pievienojiet pluszīmi (+) , kam seko % formatter , kā parādīts piemēros.

Apskatīsim, kā izmantot ar datumu saistītus formatētājus vienkāršā čaulas skriptā ar nosaukumu “date.sh”.

# PRINT YEAR,MONTH,DAY AND DATE...

echo "We are in the year = $(date +%Y)"
echo "We are in the year = $(date +%y)"

# Difference between %Y and %y is %Y will print 4 digits while %y will print the last 2 digits of the year.

echo "We are in the month = $(date +%m)"
echo "We are in the month = $(date +%b)"
echo "We are in the month = $(date +%B)"

# Difference between %B and %b is, %B will print full month name while %b will print abbreviated month name.

echo "Current Day of the month = $(date +%d)"

echo "Current Day of the week = $(date +%A)"
echo "Current Day of the week = $(date +%a)"

# Difference between %A and %a is, %A will print full Weekday name while %a will print abbreviated weekday name.

# Instead of formatting to get the date, we can use %D which will print the date as %m/%d/%y or %F which prints in %Y-%M-%d format.

echo "Date using %D = $(date +%D)"
echo "Date using %F = $(date +%F)"

Apskatīsim, kā izmantot ar laiku saistītus formatētājus vienkāršā čaulas skriptā ar nosaukumu “time.sh”.

# PRINT HOURS, MINS, SECONDS, NANO SECONDS

echo Hours = $(date +%H)
echo Minutes = $(date +%M)
echo Seconds = $(date +%S)
echo Nanoseconds = $(date +%N)
echo Epoch Time = $(date +%s)

echo "current time = $(date +%H:%M:%S:%N)"

# can also use %T which displays Time in HH:MM:SS format.

echo "current time in 24 hour format = $(date +%T)"

# can also use %r to display time in 12 hour format.

echo "current time in 12 hour format = $(date +%r)"

Izmantojot --date vai -d , karodziņa ievadi var pārsūtīt, jo virkne un datuma komanda zina, ka ar to jārīkojas prasmīgi.

Apskatīsim dažus piemērus, lai saprastu, kā tas darbojas.

# Print yesterday's date and time.
echo "Yesterday = $(date -d "Yesterday")"

# Print Tomorrow date and time.
echo "tomorrow = $(date -d "tomorrow")"

# Find what is the date and time before 10 days from now.
echo "Before 10 days = $(date -d "tomorrow -10 days")"

# Find last month and next month
echo "Last month = $(date -d "last month" "%B")"
echo "Next month = $(date -d "next month" "%B")"

# Find last year and next year
echo "Last Year = $(date -d "last year" "+%Y")"
echo "Next Year = $(date -d "next year" "+%Y")"

# Forecast the weekday
echo "2 days away from today and it comes on weekdays? = $(date -d "Today +2 days" "+%A")

aprēķiniet dienu skaitu starp 2 norādītajiem datumiem.

$ echo $(( ( $(date -d "2020-11-10" "+%s") - $(date -d "2020-11-01" "+%s") ) / 86400))

Atrodiet, ka dotais gads ir lēciena gads vai nē.

$ for y in {2000..2020}; do date -d $y-02-29 &>/dev/null && echo $y is leap year; done

Datuma komandas izvades piešķiršana mainīgajam.

$ TODAY=$(date +%Y-%m-%d)
OR
$ TODAY1=$(date +%F)
$ echo $TODAY 
$ echo $TODAY1

Izveidojiet žurnāla failus ar datumam, kas pievienots faila nosaukumam.

Datuma un laika pievienošana, veidojot žurnālfailus, dublējumkopijas vai teksta failus, ir izplatīta darbība, ar kuru mēs sastopamies visbiežāk. Ņemsim piemēru. Lai izveidotu dublējumu, mēs esam izveidojuši čaulas skriptu.

Šis skripts tiks dublēts no pulksten 00:00 līdz 23:59 un paredzēts palaist katru dienu nākamās dienas pulksten 00:00. Mēs vēlamies izveidot žurnāla failus ar vakardienas datuma formātu.

CUSTOM_FORMAT=$(date --date "Yesterday" "+%d-%y-%H:%M")
LOG_FILE=/var/log/custom_application/application_${CUSTOM_FORMAT}.log
echo "Script started" >>  ${LOG_FILE}
...
CODE BLOCKS
...
echo "Script completed" >> ${LOG_FILE}

Tas ir šis raksts. Šajā rakstā mēs esam redzējuši, kā Linux lietot bash datumu un laiku. Paziņojiet mums savas atsauksmes.