LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-04-2004, 02:04 AM   #1
icw_sec
LQ Newbie
 
Registered: Jan 2004
Posts: 4

Rep: Reputation: 0
help with Date command in script


I have a script that is searching through my syslog_ng folders to report on errors. How ever I am now not getting anything in my reports.

I am using that date command to set some variables and and reduce the day by one to search on yesterdays logs.

Code snip for setting of date info

year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
day1=$((day -1))
date=$(date +"%m_%d_%y")

After the end of the month the day1 variable is no longer keeping the 0 in front of the day. This is causing a bit of a problem.

for instance

day = 04
day1 = 3

For this report I need to maintain the 0 for the day1 variable.

thanks in advance.
 
Old 01-04-2004, 02:44 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Probably ugly if not wrong, but
Code:
if [ $day1 -lt 10 ] ; then
    echo 0$day1
else 
    echo $day1
fi
I think?

-- Well, the echo's irrelevant if that's not what you're doing with the data, but the idea of testing it as a single digit to prefix a 0, maybe. I was just echoing to see what was happening.

Last edited by slakmagik; 01-04-2004 at 02:46 AM.
 
Old 01-04-2004, 03:08 AM   #3
icw_sec
LQ Newbie
 
Registered: Jan 2004
Posts: 4

Original Poster
Rep: Reputation: 0
That did the trick. I made on small modification below. Thanks for the help.


year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
day1=$((day -1))
date=$(date +"%m_%d_%y")

if [ $day1 -lt 10 ] ; then
day2=0$day1
else
day2=$day1
fi
 
Old 01-04-2004, 03:15 AM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Cool. Oh, and I just realized these were your first posts. Welcome to LQ!
 
Old 01-04-2004, 12:41 PM   #5
kabaret
LQ Newbie
 
Registered: Jul 2003
Posts: 1

Rep: Reputation: 0
Thanks for the welcome. Been using the site for a while and have refered it to many that are just getting into linux. Just figured out I have two sign IDs.

Last edited by kabaret; 01-04-2004 at 12:44 PM.
 
Old 01-05-2004, 06:28 PM   #6
chrisk5527
Member
 
Registered: Oct 2002
Location: Michigan
Distribution: Slackware Linux 10.0
Posts: 289

Rep: Reputation: 30
Use:
---
date --date=yesterday
---

Then parse it out from there
----
Day=`date --date=yesterday | awk '{print $1}'`
----
and so on....
 
Old 01-06-2004, 11:02 AM   #7
icw_sec
LQ Newbie
 
Registered: Jan 2004
Posts: 4

Original Poster
Rep: Reputation: 0
Yet another good tip. I think for the script I am using this might be the best one. Thanks for the tip
 
  


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
'date' command format Neorio Linux - General 3 01-18-2011 05:18 AM
Trying to do something wierd with the date command cboxall Linux - Newbie 3 07-22-2005 05:24 PM
date command aetucker1 Linux - General 3 01-21-2005 12:57 PM
Date in a BASH script... soulsniper Linux - Software 7 11-22-2003 06:08 PM
can't retrieve the date in my script wedgeworth Linux - Software 0 09-15-2003 11:41 AM


All times are GMT -5. The time now is 03:18 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration