A newbie with a problem in A date Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A newbie with a problem in A date Script
# 1  
Old 09-18-2006
A newbie with a problem in A date Script

Hello everybody...
I'm a Unix newbie and i just got this task at work to figure out what's wrong with a daily script my team is using.

The idea behind the script is that it takes the day before in a yyyymmdd format, find files with that date in a specific directory and executes an (irrelavant) operation on them.
The problem with the script is that it won't work in every 1st of every month. So in every 1st of each month they need to change the script manualy.
I think that it has to do with the $d-1 section so in eache 1st of the month it thinks that the day before is 0.
My task is to figure out a way to change the script so it will work every day of the month including the 1st of every month.
Smilie
It's my first day on a new job and i dont know anything about scripting so please be gentle. I'd appreciate it if you could find a way to correct it and explain it to me.
The relevant lines are these:

d=`date '+%d'`
d=`/usr/bin/expr $d - 1`
if [ $d -lt 10 ]
then
DATE=`date '+%Y%m'`0$d
else
DATE=`date '+%Y%m'`$d
fi
DATE='20060831'
cd ${root_dir}
num=`ls -1 dec_${DATE}*|wc -l`>/dev/null
if [ $num -lt 21 ]
#if [ $num -lt 1 ]
then

Plz help...
Thx in advance !!!!
# 2  
Old 09-18-2006
Which OS do you use?

for freebsd you can replace
Quote:
d=`date '+%d'`
d=`/usr/bin/expr $d - 1`
by d=`date -v -1d +%d`
# 3  
Old 09-18-2006
Hi,

Please see this link

https://www.unix.com/showthread.php?t=29685

I had discovered a small bug in my code later. Here's the fully updated code. It takes care of previous month, previous year, leap year issues while calculating yesterday's date.

Code:
#! /bin/sh
###########################################################################################
## This scripts runs in 2 ways                                                            #
## First way: without command line arguments. It calculates yesterday's date              #
## Second way: 3 numeric argumnets(dd mm yyyy). Calculates date prior to the input date   #
###########################################################################################

case $# in
3)
   [[ `echo $1 | grep [[:alpha:]]` || `echo $1 | grep [[:punct:]]` ]] && echo -e "$1 is not a number.\nQuitting" && exit 1
   [[ `echo $2 | grep [[:alpha:]]` || `echo $2 | grep [[:punct:]]` ]] && echo -e "$2 is not a number.\nQuitting" && exit 1
   [[ `echo $3 | grep [[:alpha:]]` || `echo $3 | grep [[:punct:]]` ]] && echo -e "$3 is not a number.\nQuitting" && exit 1

   YEAR=$3
   NMONTH=$2
   DATE=$1
   # Add a 0 in front if month or date is single digit; i.e. < 10
   [[ ${#NMONTH} -eq 1 ]] && NMONTH="0$NMONTH"
   [[ ${#DATE} -eq 1 ]] && DATE="0$DATE"

   [[ $DATE > 31 || $DATE < 01 ]] && echo -e "Invalid date.\n Quitting" && exit 1
   [[ $DATE > 30 && ($NMONTH == 04 || $NMONTH == 06 || $NMONTH == 09 || $NMONTH == 11) ]] && echo -e "Invalid date.\n Quitting" && exit 1
   [[ $NMONTH > 12 || $NMONTH < 01 ]] && echo -e "Invalid month.\n Quitting" && exit 1
   [[ ${#YEAR} -ne 4 ]] && echo -e "Invalid Year.\n Quitting" && exit 1

   if [[ $NMONTH == 02 && $DATE > 28 ]]; then # Month is February, no. of days greater than 28
        if [[ `expr $YEAR % 4` != 0 ]]; then # If not divisible by 4, can't be a leap year
             echo -e "Invalid date. \n Quitting" && exit 1
        else # If divisible by 100 but not divisible by 400, can't be a leap year
           [[ `expr $YEAR % 100` == 0 && `expr $YEAR % 400` != 0 ]] && echo -e "Invalid date. \n Quitting" && exit 1
        fi
        [[ $DATE > 29 ]] && echo -e "Invalid date. \n Quitting" && exit 1
   fi

   ;;
*)
YEAR=`date +"%Y"`
NMONTH=`date +"%m"`
DATE=`date +"%d"`;;
esac
if [[ $DATE == 01 || $DATE == 1 ]]; then # If its the first day of the month, set yesterday's date accordingly.

case $NMONTH in
01|02|04|06|08|09|11)
        DATE=31
        if [[ $NMONTH == "01" || $NMONTH == "1" ]]; then
                YEAR=`expr $YEAR - 1`
                NMONTH=12;LMONTH="December"
        else
         NMONTH=`expr $NMONTH - 1`
        fi;;


03)     if [[ `expr $YEAR % 4` == 0 ]]; then #Year divisible by 4
                if [[ `expr $YEAR % 100` == 0 ]]; then #YEAR divisible by 100
                        DATE=29
                        if [[ `expr $YEAR % 400` != 0 ]]; then #YEAR not divisible by 400 is not a leap year
                                DATE=28
                        fi
                else
                     DATE=29
                fi
        else
                DATE=28
        fi
        NMONTH=`expr $NMONTH - 1`;;
*)
        DATE=30
        NMONTH=`expr $NMONTH - 1` ;;
esac

else
       DATE=`expr $DATE - 1`
fi

# If numeric month or date is <10, add a 0 in front
if [ ${#NMONTH} -eq 1 ]; then
  NMONTH="0$NMONTH"
fi
if [ ${#DATE} -eq 1 ]; then
 DATE="0$DATE"
fi

case $NMONTH in # set long month name
01)     LMONTH="January";;
02)     LMONTH="February";;
03)     LMONTH="March";;
04)     LMONTH="April";;
05)     LMONTH="May";;
06)     LMONTH="June";;
07)     LMONTH="July";;
08)     LMONTH="August";;
09)     LMONTH="September";;
10)     LMONTH="October";;
11)     LMONTH="November";;
12)     LMONTH="December";;
esac


echo $DATE-$NMONTH-$YEAR-$LMONTH #Output date-month-year


Some changes will be required to make it accept yyyymmdd date as input. currently it expects dd mm yyyy as input.
# 4  
Old 09-18-2006
[offtop]
horror Image Image Image
[/offtop]
# 5  
Old 09-18-2006
Thank you all so much !!!

Your help is appreciated !!
Thanx !!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with date on bash script

i am beginner in bash scripting and i am looking for a command that gives me one minute ago. i am working on unix ware7 and i cant use date -d or date --date command thanks for help !! (4 Replies)
Discussion started by: rezasadeghi
4 Replies

2. Shell Programming and Scripting

Shell script newbie, what is problem with my script?

Hello, Ubuntu server 11.10 can anybody help what is problem with my shell script? #!/bin/bash #script to find out currently logged on user is root or not. if ] then echo "You are super" else echo "You are awesome!" fi When I run script, I get following output ./uid: line 3: I... (4 Replies)
Discussion started by: kaustubh
4 Replies

3. Shell Programming and Scripting

php date script problem

i want to store the output of date into a variable and be able to echo out that variable in a sentence. but the below isn't working. <?php #$date_time = date('Y-m-d H:i:s'); putenv("TZ=US/Eastern"); date_time = "date('h:i:s')."\n""; echo ("<li>Today's date is: ($date_time)</li>\n"); ... (1 Reply)
Discussion started by: SkySmart
1 Replies

4. Programming

Date time problem while executing perl script.

Hi All, This Monday 15th March 2010, i have faced a weired issue with my Perl script execution, this script is scheduled to run at 1 minute past midnight on daily basis ( 00:01 EST ) generally for fetching previous business date , say if it is Monday it should give last Friday date, for Tuesday... (0 Replies)
Discussion started by: ravimishra
0 Replies

5. UNIX for Dummies Questions & Answers

Date Script problem for year cross over

Hello, I'm very new to script writing - everything I have I got off the Internet. I'm pretty sure I stole this date script from this site. Anyway, the script works great until I try to obtain a date that falls into last year. I can get 'Dec 31, 2009' but nothing earlier. Below is the... (3 Replies)
Discussion started by: Colel2
3 Replies

6. Shell Programming and Scripting

Problem - script that parses $date data...

This is probably archaic, but I'm new to unix and this is my first shell script. I'm writing this script to use in another script. All I am trying to do is make the parts of the output from date usable in my other script. I was wondering if you could stand looking at this and see if you notice... (8 Replies)
Discussion started by: Dalcron
8 Replies

7. Shell Programming and Scripting

Newbie problem with simple script to create a directory

script is: dirname= "$(date +%b%d)_$(date +%H%M)" mkdir $dirname should create a directory named Nov4_ Instead I get the following returned: root@dchs-pint-001:/=>./test1 ./test1: Nov04_0736: not found. Usage: mkdir Directory ... root@dchs-pint-001:/=> TOO easy, but what am I... (2 Replies)
Discussion started by: gwfay
2 Replies

8. Shell Programming and Scripting

Please help - newbie (date)

Hi I need help to write a script to do following: Sample Input file1: 07-01-08 08:48:07:982 INFO .... 07-01-08 08:49:07:982 DETAIL ..... 07-01-08 08:50:14:982 INFO ..... 07-01-08 08:51:23:982 DETAIL ..... 07-01-08 08:52:57:982 INFO ..... 07-01-08 08:53:01:982 DETAIL ..... 07-01-09... (1 Reply)
Discussion started by: mel_2008
1 Replies

9. Shell Programming and Scripting

Newbie convert date ksh

If I have start = 02282006; end = 03152006; How do I get startdate = 02/28/2006; enddate = 03/15/2006; Thanks, (3 Replies)
Discussion started by: britney
3 Replies

10. Shell Programming and Scripting

Newbie problem with ksh script

Hi all, I have a directory have all of the .stat and .dat file : they are is a pipe separate flat file. Example: log-20061202.stat contain 1st line and last line of log-20061202.dat with record count of that day. Example: Total record = 240 Tom|02-12-2006|1600 W.Santa... (18 Replies)
Discussion started by: sabercats
18 Replies
Login or Register to Ask a Question