Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use bash countdown date in linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use bash countdown date in linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

First of all, there are a few tips before proceeding. The% j option of the date command displays the current date as a number between 1 and 366. As you might expect, January 1 will be displayed as 1, December 31 will be displayed as 365 or 366, depending on whether it is a leap year. Keep trying. You should see the following:

$date +% j339

However, you can get the number for any day of the year in the date command in the following ways:

$date-d "Mar 18" +% j077

Keep in mind that even if the date is a past date, the above command will show you the date of the current year. However, you can add years to the command to fix the problem:

$date-d "Apr 29" +% j119 $date-d "Apr 29 2020" +% j120

In leap years, April 29 will be 120 days of the year, not 119 days.

If you want to count down the days before Christmas and don't want to leave fingerprints on the calendar, you can use the following script:

#! / bin/sh XMAS= `date-d "Dec 25" +% j`ToDAY = `date +% j`DAYS = $($XMAS-$TODAY)) case $DAYS in 0) echo "It's today! Merry Christmas!";; [0-9] *) echo "$DAYS days remaining";;-[0-9] *) echo "Oops, you missed it";; esac

In this script, we get the dates of December 25 and today, and then subtract them. If the result is positive, we will display the number of days remaining. If zero, a "Merry Christmas" message is sent, and if negative, only the person running the script is told that they missed the holiday. Maybe they're addicted to eggnog.

The case statement consists of statements used to print information, when the remaining time is equal to 0, or any number or number that begins with the-symbol (that is, the past) prints different information.

You can use the same method for any date that people want to focus on. In fact, we can ask the person running the script to provide a date and let them know how many days there are between now and that day. The script goes like this.

#! / bin/sh echo-n "Enter event date (e.g., June 6):" read dtEVENT= `date-d "$dt" +% j`date +% j`DAYS = `expr $EVENT-$TODAY` case $DAYS in 0) echo "It's today!";; [0-9] *) echo "$DAYS days remaining";-[0-9] *) echo "Oops, you missed it"; esac;

One problem with using this script is that people who run it will be disappointed if they want to know how many days there are left on this special day of the next year. Even if they provide the year when they enter the date, the date-d command will only provide the number of days in the middle of the year, not the number of days between now and then.

Calculating the number of days between today and the date of a certain year can be tricky. You need to include all intermediate years and pay attention to those leap years.

Use Unix Epoch time

Another way to calculate the number of days between now and a particular date is to use the Unix system to store dates. If you convert the number of seconds from January 1, 1970 to days, you can easily do this, as shown in the following script:

#! / bin/bash echo-n "Enter target date (e.g., Mar 18 2021) >" read target_datetoday= `echo $(($(date-- utc-- date "$1" +% s) / 86400)) `target= `echo $(($(date-- utc-- date "$target_date" +% s) / 86400)) `days= `expr $target-$today`echo "$days days until $echo"

To explain, 86400 is the number of seconds in a day. The number of days is divided by the number of seconds since the beginning of the Unix era.

$. / countdownEnter target date (e.g., Mar 18 2021) > Mar 18 2020104 days until Mar 18 2020 Thank you for reading this article carefully. I hope the article "how to use bash countdown in linux" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report