The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Difference in day-hours-minutes-seconds format Sreejith_VK Shell Programming and Scripting 7 02-27-2008 02:04 AM
Adding # minutes to current time... gptavares UNIX for Advanced & Expert Users 7 06-25-2007 04:10 PM
crontab every 2 minutes, 24 hours and once a week Bobafart UNIX for Dummies Questions & Answers 6 05-17-2007 11:08 AM
Convert minutes to hours, minutes, seconds Vozx Shell Programming and Scripting 1 12-09-2005 01:24 AM
How can I create a file with current time - 60 minutes DaveyTN Shell Programming and Scripting 4 10-05-2005 09:17 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-17-2009
SGD SGD is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 1
Reading Hours and Minutes from file and comparing with current

Hi,

Time till when the application should run is indicated in a file. First line is hour and second line is minute.
file:
10
55
Means my application should run till 10:55.

Now in a shell script, i am trying to make that logic but with no luck.

min=`tail -n 1 /file_with_time`
hour=`head -n 1 /file_with_time`

chour=`date +%H`
cmin=`date +%M`

if [ $chour < $hour ] <== Shell script throws error here when value is two digits ie if first line the file is > 9.
then
run_my_app
fi

if [ $chour = $hour ]
then if [ $cmin < $min ]
then
run_my_app
fi
fi

Kindly suggest.
SDG
  #2 (permalink)  
Old 03-17-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,362
Quote:
Originally Posted by SGD View Post
Hi,

Time till when the application should run is indicated in a file. First line is hour and second line is minute.
file:
10
55
Means my application should run till 10:55.

Now in a shell script, i am trying to make that logic but with no luck.

Please put code inside [code] tags.
Quote:

Code:
min=`tail -n 1 /file_with_time`
hour=`head -n 1 /file_with_time`

You don't need two external commands to read two lines from a file:


Code:
{
 read min
 read hour
} < /file_with_time

Quote:

Code:
chour=`date +%H`
cmin=`date +%M`

You don't need two calls to date (and it will be wrong if the hour changes between one call and the next).


Code:
eval "$( date "+hour=%H min=%M" )"

Quote:

Code:
if [ $chour < $hour ] <== Shell script throws error here when value is two digits ie if first line the file is > 9.
then
   run_my_app
fi

if [ $chour = $hour ]
then if [ $cmin < $min ]

The less-than operator is -lt:


Code:
if [ $cmin -lt $min ]

But you could also do it with a single if statement:


Code:
if [ $chour$cmin -lt $hour$min ]

Quote:

Code:
then
  run_my_app
fi
fi
Closed Thread

Bookmarks

Tags
read numbers and use in if conditions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0