comparing two decimal values in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparing two decimal values in KSH
# 1  
Old 11-14-2005
comparing two decimal values in KSH

Hi Gurus,

I wrote a small KSH script, in that i am comparing two variables like this

curr_time = date +%h.%m
set_time=23.55

If [[$curr_time -ge $set_time]];
then
zip the file

fi

The above script is croned to run evey 5(3,8,.......,58) minutes, but it is zipping at 23.03 hours. My intention is at 23.58 .what is wrong here. Please help me
# 2  
Old 11-14-2005
Quote:
Originally Posted by nandinisagar
Hi Gurus,

I wrote a small KSH script, in that i am comparing two variables like this

curr_time = date +%h.%m
set_time=23.55

If [[$curr_time -ge $set_time]];
then
zip the file

fi

The above script is croned to run evey 5(3,8,.......,58) minutes, but it is zipping at 23.03 hours. My intention is at 23.58 .what is wrong here. Please help me
several issues:
Code:
#!/bin/ksh

typeset -i curr_time=$(date +%H%M)
typeset -i set_time=2355

if (( $curr_time >= $set_time ));
then
   echo 'zip the file'
fi

# 3  
Old 11-15-2005
I am in the learning phase... Can't we compare decimals and can't we use -GE instead of >=. Please educate me
# 4  
Old 11-15-2005
ksh [the ksh88 version] doesn't support floating point arithmetics.
# 5  
Old 11-15-2005
Comparing decimal numbers

Quote:
Originally Posted by nandinisagar
I am in the learning phase... Can't we compare decimals and can't we use -GE instead of >=. Please educate me
Sadly, the shell can't do it. Gladly, though, the shell along with its tools can! You have to think of shell programming as combining your bits and pieces into a whole that's greater than the sum of its parts.

That doesn't mean the shell isn't ugly and clumsy. It is. Even moreso than Perl. But it sure is convenient, and reasonably powerful!

Here's one way to do what you want:

curr_time = date +%h.%m
set_time=23.55

result=`echo "if ( $curr_time > $set_time) 1" | bc`

If [[ "$result" = 1 ]];
then
zip the file

fi
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Arithmetic syntax error while comparing decimal numbers

Hello, I am having a problem when i execute following script on RHEL 6.4. Same script works fine on another machine where I have same version of RHEL and KSH. Below is the rpm and RHEL version. ossvm12(0)> rpm -qa | grep ksh ksh-20100621-19.el6.x86_64 ossvm12(0)> cat... (7 Replies)
Discussion started by: Adithya Gokhale
7 Replies

2. Shell Programming and Scripting

Comparing decimal numbers between 0 and 1

For numbers between 0 and 1 the below logic is not working. Output of above shall be "correct" but its echoing "incorrect".Kindly suggest a=.1 if then echo correct else echo incorrect fi Video tutorial on how to use code tags in The UNIX and Linux Forums. (3 Replies)
Discussion started by: itsvikas
3 Replies

3. Shell Programming and Scripting

Working With Values After Decimal

I have two files which have to be compared. One of them has leading & trailing zeroes in certain fields. file1 ---- John,Rambo,20100101,2119.5,3302.39,100.07,22211.0 file2 ---- John,Rambo,20100101,000002119.50,0003302.39,00000.07,000022211.00 I am thinking of using diff to... (10 Replies)
Discussion started by: Sheel
10 Replies

4. Shell Programming and Scripting

How to get decimal values ?

Hi All, In my script I've written like this- c=$( expr 100 / 3);echo $c The output coming is 33. but I want to see 33.33, decimal values too. How to get that? Thanks, Naresh (3 Replies)
Discussion started by: NARESH1302
3 Replies

5. Shell Programming and Scripting

Comparing two numbers with decimal point

How to compare two numbers with decimal points ? Is there a way in bash to do this? (33 Replies)
Discussion started by: kinny
33 Replies

6. Shell Programming and Scripting

Comparing Decimal Numbers

Im trying to compare two numbers with decimals but its not working as expected. a=1 b=1.1 if then echo "equal" fi When I do this it says that the numbers are equal. Ultimately Im using -le and -ge in the if statements but I tested with -eq for simplicity. Any way to make this... (3 Replies)
Discussion started by: Grizzly
3 Replies

7. Shell Programming and Scripting

format decimal in ksh

hi all, in ksh, how do i format a decimal number (e.g 3381.19) to integer (e.g 3381). This number is average time response for server in millisec and i wanted to display it in a more readable format i.e in seconds without any decimals. thanks in advance. (2 Replies)
Discussion started by: cesarNZ
2 Replies

8. Shell Programming and Scripting

Evaluating Decimal values

How can I evaluate a decimal value in an if statement? echo "Enter limit:" read limit (enter a decmal value, ie: 2.5) decimallimit=`echo $limit+0|bc|quit` echo $decimallimit if then echo $decimallimit else echo "failed" fi (4 Replies)
Discussion started by: larrys721
4 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies
Login or Register to Ask a Question