Linux脚本-日期操作 [英] Linux Script- Date Manipulations

查看:90
本文介绍了Linux脚本-日期操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将设置一个日期变量(例如'08 -JUN-2011'),我想基于该日期进行一些计算,
1.必须获得给定月份的第一天.
2.给定日期月份的上一个日期.
3.给定日期月份的最后一天.

I will set one date variable(Say '08-JUN-2011') and I want to do some calculations based on that date namely,
1. Have to get the first day of the given day's month.
2. Previous date of the given date's month.
3. Last day of the given date's month.

我所知道的是使用当前系统日期和时间进行操作,但不知道如何使用用户定义的日期来实现.我需要使用Linux Shell脚本来实现.

All I know is manipulating using the current system date and time but don't know how to implement with user defined date. I need this to be achieved using Linux shell script.

任何帮助将不胜感激.

谢谢,
卡尔提克

Thanks,
Karthik

推荐答案

以下是使用GNU日期执行操作的方法:

Here's how to perform the manipulations using GNU date:

#!/bin/sh

USER_DATE=JUN-08-2011

# first day of the month
FIRST_DAY_OF_MONTH=$(date -d "$USER_DATE" +%b-01-%Y)

PREVIOUS_DAY=$(date -d "$USER_DATE -1 days" +%b-%d-%Y)

# last day of the month
FIRST_DAY_NEXT_MONTH=$(date -d "$USER_DATE +1 month" +%b-01-%Y)
LAST_DAY_OF_MONTH=$(date -d "$FIRST_DAY_NEXT_MONTH -1 day" +%b-%d-%Y)

echo "User date: $USER_DATE"
echo "1. First day of the month: $FIRST_DAY_OF_MONTH"
echo "2. Previous day: $PREVIOUS_DAY"
echo "3. Last day of the month: $LAST_DAY_OF_MONTH"

输出为:

User date: JUN-08-2011
1. First day of the month: Jun-01-2011
2. Previous day: Jun-07-2011
3. Last day of the month: Jun-30-2011

这篇关于Linux脚本-日期操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆