Otu esi arụ ọrụ na ụbọchị na oge na Bash Iji ụbọchị iwu
Iwu ụbọchị bụ mmemme bash mpụga nke na-enye ohere ịtọ ma ọ bụ gosipụta ụbọchị na oge sistemụ. Ọ na-enyekwa ọtụtụ nhọrọ nhazi. Awụnyere iwu ụbọchị na Linux distros niile na ndabara.
$ which date $ type -a date
Pịnye iwu ụbọchị na ọnụ nke ga-egosipụta ụbọchị na oge dị ugbu a.
$ date
N'iji iwu ụbọchị, ụbọchị sistemụ, oge na mpaghara oge nwere ike gbanwee yana mgbanwe ahụ ga-ejikọta ya na elekere ngwaike.
$ date --set="Thu Nov 12 13:06:59 IST 2020" $ hwclock --systohc
Ebe dị mma iji nweta ndepụta nke nhọrọ nhazi ga-abụ ibe nwoke.
$ man date
Ka anyị hụ ụfọdụ nhọrọ nhazi usoro a na-ahụkarị nke anyị ga-eji.
- Iji tinye usoro were “+ na-esote “formatter“.
- I nweta ndepụta nhọrọ nhazi maka GNU\LINUX, lelee ibe nwoke ejikọtara.
- Iji nweta ndepụta nhọrọ nhazi maka BSD, lelee ibe nwoke ejikọtara.
Akụkụ abụọ dị mkpa nke iwu ụbọchị bụ iji Ụdị +%
na -date nhọrọ.
Ugbu a, ka anyị tinye ụfọdụ nhazi na iwu ụbọchị. Iji tinye usoro nhazi, tinye gbakwunyere akara (+)
sochiri %formatter
dị ka egosiri na ọmụmaatụ.
Ka anyị leba anya ka esi eji ndị na-emepụta ụbọchị metụtara ya na edemede shei dị mfe akpọrọ '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)"
Ka anyị leba anya ka esi eji ndị nhazi oge na-eme ihe na edemede shei dị mfe akpọrọ '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)"
Na --date
ma ọ bụ -d
nwere ike gafere ndenye ọkọlọtọ dị ka eriri na ụbọchị iwu maara ijikwa ya nke ọma.
Ka anyị hụ ihe atụ ụfọdụ iji ghọta otú o si arụ ọrụ.
# 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")
gbakọọ ọnụọgụ ụbọchị n'etiti ụbọchị abụọ enyere.
$ echo $(( ( $(date -d "2020-11-10" "+%s") - $(date -d "2020-11-01" "+%s") ) / 86400))
Chọta afọ enyere bụ afọ leap ma ọ bụ na ọ bụghị.
$ for y in {2000..2020}; do date -d $y-02-29 &>/dev/null && echo $y is leap year; done
Na-ekenye mmepụta nke iwu ụbọchị na mgbanwe.
$ TODAY=$(date +%Y-%m-%d) OR $ TODAY1=$(date +%F) $ echo $TODAY $ echo $TODAY1
Mepụta faịlụ ndekọ na ụbọchị agbakwunyere na aha faịlụ.
Ịgbakwunye ụbọchị na oge ka ị na-eke faịlụ ndekọ, ndabere, ma ọ bụ faịlụ ederede bụ ọrụ a na-ahụkarị nke anyị ga-ezute ọtụtụ mgbe. Ka anyị were ihe atụ, iji were ndabere, anyị ekepụtala edemede shei.
Edemede a ga-eweghachi ndabere site na 00:00 ruo 23:59 ma hazie ya ka ọ na-agba kwa ụbọchị na 00:00 nke ụbọchị na-esote. Anyị chọrọ ịmepụta faịlụ ndekọ na usoro ụbọchị ụnyaahụ.
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}
Nke ahụ bụ maka akụkọ a. N'isiokwu a, anyị ahụla ka esi eji ụbọchị na oge bash na Linux. Mee ka anyị mara nzaghachi gị.