LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-15-2013, 10:05 PM   #1
learne
LQ Newbie
 
Registered: Jul 2013
Posts: 7

Rep: Reputation: Disabled
I have question on shell scripting


Hi Experts,

I have question on shell scripting, how can i get the first day of a month based on the date supplied.

for example, based on current date how can i get the first day of the month in.



thanks
 
Old 07-15-2013, 10:22 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
one way to do it
Code:
ncal | awk '$2 == "1" {print $1}'

Code:
Month=05
Year=2000
ncal -m $Month $Year | awk '$2 == "1" {print $1}'
but that isn't really shell scripting
 
Old 07-15-2013, 10:32 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Just go through manual of follow comamnds:
http://linux.die.net/man/1/date
http://linux.die.net/man/1/cal

Just for hint, to display the current month:
Code:
~$ date +%m
To display the current month's calander:
Code:
~$ cal `date +%m` `date +%Y`
Then using these comamnds/combinations, you can extract the first day of month. So give it a try.
 
Old 07-15-2013, 10:44 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

the first day of the month is the "1st".

Perhaps you want the day of the week for the first day of the current month:

Code:
date +%A --date=$(date +%Y-%m-01)
Or for a specific year and month Eg March 2012.
Code:
date +%A --date=2012-03-01
If this isn't what you want, can you please restate the question?

Cheers,

Evo2.
 
1 members found this post helpful.
Old 07-15-2013, 11:11 PM   #5
learne
LQ Newbie
 
Registered: Jul 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi Experts,

i want the first days date of the month.

for example

if date is 15072013, i want 01070213 in dd/mm/yyyy format

so based on the date supplied as input i want to fetch its first day date.

regards,
 
Old 07-15-2013, 11:29 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by learne View Post
Hi Experts,

i want the first days date of the month.

for example

if date is 15072013, i want 01070213 in dd/mm/yyyy format

so based on the date supplied as input i want to fetch its first day date.

regards,
Well, that is a little unexpected

Code:
Date=15072013
echo $Date | echo 01$(cut -c 3-)
Would do what you want, but is it what you need?

evo2's is better,

Code:
Date=15072013
date +%d/%m/%Y --date=$(echo $Date | cut -c 5-)-$(echo $Date | cut -c 3-4)-01

Code:
man date
Will help with the formatting of the output


But at the end of the day it depends where you get, and in what form your input is and what you want to do with it


Quote:
Originally Posted by learne View Post
if date is 15072013, i want 01070213 in dd/mm/yyyy format
do you want 01/07/2013 or 01072013 ?
 
Old 07-15-2013, 11:36 PM   #7
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
dirty I know
echo 15072013 | awk -F "" {'print "01/"$3$4"/"$5$6$7$8'}
 
Old 07-15-2013, 11:41 PM   #8
learne
LQ Newbie
 
Registered: Jul 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
#!/usr/bin/ksh

datestamp=`date '+%Y%m%d'`
yest=$((datestamp -1))
echo $yest

when i execute the above code i got the below output:
Output: 20130715

How can i display the same output in 15/07/2013, i tried with the format '+%d/%m/%y'` but i am getting error.

also how can i get the first day of the month based on the date which is fetched $yest=20130715.

for example: 01/07/2013 which is first day on the monthe

Any help.

Thanks
 
Old 07-16-2013, 12:07 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by learne View Post
#!/usr/bin/ksh

datestamp=`date '+%Y%m%d'`
yest=$((datestamp -1))
echo $yest

when i execute the above code i got the below output:
Output: 20130715

How can i display the same output in 15/07/2013, i tried with the format '+%d/%m/%y'` but i am getting error.

also how can i get the first day of the month based on the date which is fetched $yest=20130715.

for example: 01/07/2013 which is first day on the monthe

Any help.

Thanks
OK, this is Dirty..
Code:
#!/usr/bin/ksh
function Yest {
Year=$1
Month=$2
Day=$3
YDay=$(expr $Day - 1)

date '+%A %d/%m/%Y' --date=${Year}-${Month}-${Day}
date '+%A %d/%m/%Y' --date=${Year}-${Month}-${YDay}
date '+%A %d/%m/%Y' --date=${Year}-${Month}-01
}

Yest $(date '+%Y %m %d')
passing 3 arguments to a function
The arguments are the output of date formatted such that we have spaces between year month and day
this is so we can easily manipulate the day ( to get yesterday )

Hope that makes sense

Last edited by Firerat; 07-16-2013 at 12:16 AM. Reason: added day of week to justfy using date instead of just echo/printf
 
Old 07-16-2013, 12:20 AM   #10
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
If you're just concerned about printng the first date of given months in ddmmyyyy format according to provided date as input, then it can be done simply as:
Code:
#!/bin/bash
echo "Insert date: "; read mydate   ## Here you have to provide date format e.g. 15072013 as your input
month=$(echo $mydate| cut -c 3-4)
year=$(echo $mydate | cut -c 5-8)
echo 01$month$year

Last edited by shivaa; 07-16-2013 at 12:21 AM.
 
Old 07-16-2013, 12:22 AM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
ooops , what if date was 01/07/2013
yesterday would be 00 ???

so if YDay=0, must reduce Month by 1, and set YDay to last day of month.

Edit
getting yesterday is easy

[date]
date +%d/%m/%Y --date="yesterday"[/code]

Last edited by Firerat; 07-16-2013 at 12:34 AM.
 
Old 07-16-2013, 12:33 AM   #12
learne
LQ Newbie
 
Registered: Jul 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Hi Experts,

The question is i am trying to fetch the previous day using current date. for example if current date is 16/07/2013, i want previous day's date. i.e., 15/07/2013.

using current date fetch yesterday's date.

Input current date and output is current date - 1 - 15/07/2013.

how can i get this?

any suggestions
 
Old 07-16-2013, 12:35 AM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by learne View Post
Hi Experts,

The question is i am trying to fetch the previous day using current date. for example if current date is 16/07/2013, i want previous day's date. i.e., 15/07/2013.

using current date fetch yesterday's date.

Input current date and output is current date - 1 - 15/07/2013.

how can i get this?

any suggestions
Turns out it is easy

Code:
date +%d/%m/%Y --date="yesterday"
 
Old 07-16-2013, 12:42 AM   #14
learne
LQ Newbie
 
Registered: Jul 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Firerat View Post
Turns out it is easy

Code:
date +%d/%m/%Y --date="yesterday"

Hi,

When i executed the above code, i got the output as:

date +%d/%m/%Y --date="yesterday"
16/07/2013

i want previous date, i am new to scripting.
 
Old 07-16-2013, 12:48 AM   #15
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by learne View Post
Hi,

When i executed the above code, i got the output as:

date +%d/%m/%Y --date="yesterday"
16/07/2013

i want previous date, i am new to scripting.
What time is it where you are ?

do
Code:
date
date --date="yesterday"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell scripting question Komelore Linux - Software 3 03-12-2007 04:21 AM
shell scripting question bhert Linux - General 2 01-29-2007 08:55 PM
shell scripting question creolophus Linux - Software 1 10-02-2006 02:13 AM
Shell Scripting Question Onyx^ Linux - General 5 04-27-2004 10:37 AM
Shell Scripting Question jester_69 Programming 13 11-05-2003 06:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration