The UNIX and Linux Forums  

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




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #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,378
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