comparison problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting comparison problem
# 1  
Old 11-03-2005
comparison problem

Hi,
Is there any limitation on the no of characters to be compared using the if statement in Unix. We had an issue while comparing the following two nos.
var1=20051031222900 & var2=20051101003545. The last six dgits are the time stamp for a day.
The if statement is like this:
if [ $var2 -gt $var1 ]
then
move files to a diff directory
else
dont move
fi

The script was coming to the else path for the above mentioned comparisons.
We changed the variable to :
var1=051031222900 and var2=051101003545. After this change, the if statement worked fine. Could anyone please throw some light on this.
Our unix box is HP-UX 11.11.

Thanks,
Ranjith
# 2  
Old 11-03-2005
got the solution

Guys,
I searched through the site and got the solution. It could be done through string comparisons.
if [[ $var2 > $var1 ]]
then
--
else
--
fi
Thanks a lot guys.

Ranjith Smilie
# 3  
Old 11-05-2005
some info needed

Hi all,
Can some one throw some light on how "if" compares nos. Just needed to follow up on the above mentioned issue and why it failed.
Thanks in advance,
Ranjith
# 4  
Old 11-05-2005
ksh converts the strings to integers. Then it does an arithmetic compare on the integers. With HP-UX 11.11, ksh is using 32 bit integers.
# 5  
Old 11-05-2005
Thanks perderabo

Thanks Perderabo. Found out the issue. Looks like the size of the variable was higher than that could be stored and it overflowed. Is it better doing string comparisons in such cases so that such a one-off issue doesn't arise.
# 6  
Old 11-05-2005
String compare on numbers is dangerous...
Code:
$ [ 9 > 11 ] && echo yikes!
yikes!
$

You are getting away with it because your strings have the same length.
# 7  
Old 11-05-2005
if strings are same length

What happens if the strings to be compared are of the same length in the application all the time. Then is it advisable. There is going to be no change in the length of both the strings.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help about comparison

Hello folks, I have two files, which have usernames, I want to see the contents of file1.txt which is missing in file2.txt and another comparison file2.txt contents which is missing in file1.txt. please suggest. file1.txt user u2 u8 a9 p9 p3 u4 z8 aaa ahe oktlo (7 Replies)
Discussion started by: learnbash
7 Replies

2. Shell Programming and Scripting

Problem in string comparison

guys , i am using inotify for monitoring one directory to check core file generation , my snippet of code is follows #!/bin/bash DIR=$1 inotifywait -q -e create -m $DIR | while read path events name; do if ]; then echo "Now I am going to do something with $name in directory $path."... (5 Replies)
Discussion started by: baker
5 Replies

3. Shell Programming and Scripting

awk Help - Comparison Operator problem

Hi, I've tried searching through the forum but I've drawn a blank so i'm going to post here. I'm developing a number of checks on a CSV file, trying to find if any are greater than a max limit. I'm testing it by running it from a command line. The file I'm testing has 8 records. When I... (3 Replies)
Discussion started by: Tmart
3 Replies

4. Shell Programming and Scripting

String comparison problem

Hi, can someone please help me!!! urgent! I have a strange issue here. I grep for 2 strings from a txt files and compare the string value. Though the string values are the same, they are compared as different values. Please help Case-1 -------- Here I grep for 2 different field values... (3 Replies)
Discussion started by: vani123
3 Replies

5. Shell Programming and Scripting

Three Difference File Huge Data Comparison Problem.

I got three different file: Part of File 1 ARTPHDFGAA . . Part of File 2 ARTGHHYESA . . Part of File 3 ARTPOLYWEA . . (4 Replies)
Discussion started by: patrick87
4 Replies

6. Shell Programming and Scripting

problem in string comparison in shell programming

Hello, was just wondering how to compare strings in unix? I mean as in C there is a function strcmp() in string.h, is there any function in unix for that? I tried using if and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance :) (9 Replies)
Discussion started by: salman4u
9 Replies

7. Shell Programming and Scripting

need some help..Comparison

I need some help which would probably be for most of you a simple script. I need to read in the data from a .dat file and then compare avg to see who is the highest avg. Here is my script so far. #!/bin/ksh #reading in the data from lab3.dat filename=$1 while read name o1 o2 o3 o4 o5 o6... (0 Replies)
Discussion started by: bluesilo
0 Replies

8. AIX

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (1 Reply)
Discussion started by: amarnath
1 Replies

9. Shell Programming and Scripting

Problem in ksh script ( String comparison )

hi , i am trying to compre two strings if ] or if ] when the length of var1 is small (around 300-400 char ) it works fine but when it is large (around 900-1000 chars) it fails is there any limitations for this type of comparison ??? (3 Replies)
Discussion started by: amarnath
3 Replies

10. UNIX for Dummies Questions & Answers

character comparison problem

I have three lines in a log that I want to check to make sure that they say the following (mm/dd/yyyy and hh:mm:ss changes depending in when the log is generated): '*** Testing file for mm/dd/yyyy' '*** End of procedure! Time was: hh:mm:ss' '*** End ***' I did some sed... (13 Replies)
Discussion started by: giannicello
13 Replies
Login or Register to Ask a Question