Sponsored Content
Top Forums Shell Programming and Scripting if [ $NOWDATE -gt $STARTDATE ] , date comparison correct syntax? Post 302369356 by durden_tyler on Saturday 7th of November 2009 05:24:01 PM
Old 11-07-2009
Quote:
Originally Posted by danpaluska
...
Code:
# a bunch of initializition steps are here ...
STARTDATE=`date +%Y%m%d`
 
while [ 1 ] # loop running all the time. 
do

NOWDATE=`date +%Y%m%d`

# if day changes then i want to reinitialize
if [ $NOWDATE -gt $STARTDATE ] 
then
# do all that initiallization again 
fi

# ... a bunch of other code in here

done

but that if statement doesn't work. any tips?
...
Did you wait long enough ?

The value of NOWDATE will change when the day changes. So if you start your script and keep it running till midnight (when the day changes), the "if" condition will return true thereafter.

Code:
$
$ cat -n t1.sh
     1  #!/bin/bash
     2
     3  # a bunch of initializition steps are here ...
     4  STARTDATE=`date +%Y%m%d`
     5  echo "STARTDATE = $STARTDATE"
     6
     7  NOWDATE=`date +%Y%m%d`
     8  if [ $NOWDATE -gt $STARTDATE ]; then
     9    echo "in if (-gt)  : NOWDATE = $NOWDATE"
    10  else
    11    echo "in else (-le): NOWDATE = $NOWDATE"
    12  fi
    13
    14  NOWDATE="20091108"
    15  if [ $NOWDATE -gt $STARTDATE ]; then
    16    echo "in if (-gt)  : NOWDATE = $NOWDATE"
    17  else
    18    echo "in else (-le): NOWDATE = $NOWDATE"
    19  fi
    20
$
$ . t1.sh
STARTDATE = 20091107
in else (-le): NOWDATE = 20091107
in if (-gt)  : NOWDATE = 20091108
$
$

tyler_durden
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Correct Syntax For Calling Shell Script in Perl Module

Problem: I have a shell script that will be called by a Perl module that will connect to a db and delete rows. The Perl module will be called by CRON. I am using a Perl module to call a shell script because I need to get the db connection from Perl. Here is the Perl pseudocode: ... (4 Replies)
Discussion started by: mh53j_fe
4 Replies

2. Shell Programming and Scripting

Plz correct my syntax of shell script

Dear all I am still bit new in shell script area.I am writing down a shell script which I guess somewhere wrong so please kindly correct it. I would be greatful for that. What I actually want from this shell script is that it will move all the files one by one to another server which can be... (2 Replies)
Discussion started by: girish.batra
2 Replies

3. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

4. UNIX Desktop Questions & Answers

Correct syntax

Hi, I want to check if file(s) exist even in subdirectories and perform an action. After searching here couldn't find solution that would work, but made my own solution that works fine: if then echo egrep "$1|$2|$3" `find| grep MLOG` else echo "MLOG does not exist" fiThat will check... (1 Reply)
Discussion started by: Vitoriung
1 Replies

5. Shell Programming and Scripting

Do syntax is correct ?

I tried with sed command to create a space between namespace from the XML file. I used this syntax. Can someone tell me is this syntax is vaild? /usr/xpg4/bin/sed -e 's/<\/^.*><^.:Errort>/<\/^.*> <^.:Errort>/g' test > test2 I dint find any changes or any space being created between... (10 Replies)
Discussion started by: raghunsi
10 Replies

6. UNIX for Dummies Questions & Answers

Unable to convert date into no. using date -d +%s syntax in ksh shell

hi friends, I m trying to write a script which compares to dates. for this i am converting dates into no using synatx as below v2=`date | awk '{print $2,$3,$4}'` v3=`date +%s -d "$v2"` this syntax is working in bash shell ,but fails in ksh shell. please suggest on this. (12 Replies)
Discussion started by: Jcpratap
12 Replies

7. Shell Programming and Scripting

Cannot find correct syntax to make file name uppercase letters

I have a file name : var=UsrAccChgRpt I want to make them upper case. Tried: $var | tr Error: tr: Invalid combination of options and Strings. Usage: tr | -ds | -s | -ds | -s ] String1 String2 tr { -d | -s | -d | -s } String1 Could you please help. I am using AIX... (2 Replies)
Discussion started by: digioleg54
2 Replies

8. OS X (Apple)

Can't figure out the correct syntax for a command loading a webkit plugin

Hello, Using Bash on Mac OS X 10.7.5 (Lion). I downloaded a GrowlSafari plugin for Webkit from its GitHub page GitHub - uasi/growl-safari-bridge: GrowlSafariBridge enables arbitrary javascript (including Safari Extensions) to notify via Growl.. In the description it says that after installing for... (0 Replies)
Discussion started by: scrutinizerix
0 Replies

9. Shell Programming and Scripting

Date not correct so it changed to current date.

Hello, so basically i have a txt file containing "foto's" named YYYY-MM-DD_HH.mm.ss.jpeg. But since it can probably not convert it it changes the date to the current date. What am i doing wrong? #!/bin/bash inputDateFmt() { sed -e 's/_/ /g' -e 's/\./:/g' <<< "$1" } begindatum=$(date... (3 Replies)
Discussion started by: Rovester
3 Replies

10. UNIX for Beginners Questions & Answers

How to form a correct syntax to sift out according to complementary patterns with 'find'?

I need to find all files and folders containing keyword from the topmost directory deep down the tree but omitting all references to keyword in web-search logs and entries, i.e. excluding search and browsing history made using web-browser1, web-browser2, web-browser3, (bypassing all entries of the... (8 Replies)
Discussion started by: scrutinizerix
8 Replies
DATE(1) 						      General Commands Manual							   DATE(1)

NAME
date - print or set the date and time SYNOPSIS
date [-qsu] [[MMDDYY]hhmm[ss]] [+format] OPTIONS
-q Read the date from stdin -s Set the time (implicit for -q or a date string) -u Print the date as GMT -t Use this number of seconds instead of current time EXAMPLES
date # Print the date and time date 0221921610 # Set date to Feb 21, 1992 at 4:10 p.m. DESCRIPTION
With the -q flag or a numeric argument, date sets the GMT time and date. MMDDYY refers to the month, day, and year; hhmmss refers to the hour, minute and second. Each of the six fields must be exactly two digits, no more and no less. date always display the date and time, with the default format for the system. The -u flag request GMT time instead of local time. A format may be specified with a + followed by a printf-like string with the following options: %% % character %A Name of the day %B Name of the month %D mm/dd/yy %H Decimal hour on 2 digits %I Decimal hour modulo 12 on 2 digits %M Decimal minute on 2 digits %S Decimal seconds on 2 digits %T HH:MM:SS %U Decimal week number, Sunday being first day of week %W Decimal week number, Monday being first day of week %X Same as %T %Y Decimal year on 4 digits %Z Time Zone (if any) %a Abbreviated name of the day %b Abbreviated name of the month %c Appropriate date & time (default format) %d Decimal day of the month on 2 digits %e Same as %d, but a space replaces leading 0 %h Same as %b %j Decimal dey of the year on 3 digits %m Decimal month on 2 digits %n Newline character %p AM or PM %r 12-hour clock time with AM/PM %s Number of seconds since the epoch %t Tab character %w Decimal day of the week (0=Sunday) %x Same as %D %y Decimal year on 2 digits SEE ALSO
time(2), ctime(3), readclock(8). DATE(1)
All times are GMT -4. The time now is 02:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy