bash problem when comparing integers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash problem when comparing integers
# 8  
Old 02-20-2009
Most shells won't hand integers that big.
In the case of a reversed date string a text compare should do.

Code:
#!/bin/ksh
currentfiledate="20090220080101"
reportfiledate="20090219231245"

if [ "${currentfiledate}" -gt "${reportfiledate}" ]
then
        echo "message"
fi

# 9  
Old 02-20-2009
Hi,

Yes, the reversed date string compare works but the problem is that I have to add this logic to a bash script and the bash script will not handle "-gt" when comparing strings.

Thank you.
# 10  
Old 02-20-2009
If there was a problem with the size of the integer, I should also have a problem changing their values, adding to them, But this code runs without error except when it is time to compare the variables.

#!/usr/local/bin/bash
currentfiledate=20090220080101
reportfiledate=20090219231245

currentfiledate=$(expr $currentfiledate + 1001)
reportfiledate=$(expr $reportfiledate + 1001)

echo $currentfiledate
echo $reportfiledate


if [ $currentfiledate -gt $reportfiledate ] ; then
echo -ne "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n"
echo -ne "!! Running the load utility !! \n"
fi

Results from 'echo':
20090220081102
20090219232246


The smallest values I can use, when comparing without error, are between 1 and 999999999.

Thanks.
# 11  
Old 02-20-2009
If there was a problem with the size of the integer, I should also have a problem changing their values, adding to them, But this code runs without error except when it is time to compare the variables.

#!/usr/local/bin/bash
currentfiledate=20090220080101
reportfiledate=20090219231245

currentfiledate=$(expr $currentfiledate + 1001)
reportfiledate=$(expr $reportfiledate + 1001)

echo $currentfiledate
echo $reportfiledate


if [ $currentfiledate -gt $reportfiledate ] ; then
echo -ne "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n"
echo -ne "!! Running the load utility !! \n"
fi

Results from 'echo':
20090220081102
20090219232246


The smallest values I can use, when comparing without error, are between 1 and 999999999.

Thanks.
# 12  
Old 02-20-2009
Try using the shell keyword [[ instead of the [ builtin:

Code:
[[ $currentfiledate -gt $reportfiledate ]]

Or better use the (( )) compound command:

Code:
(( "$currentfiledate" > "$reportfiledate" ))

If you're on Solaris, you may consider using the POSIX shell /usr/xpg4/bin/sh instead of this old version of bash,
notice that with that shell you should use the standard syntax: [ expression ].

Last edited by radoulov; 02-20-2009 at 01:18 PM..
# 13  
Old 02-20-2009
Hi radoulov,

It works, I tried both ways with bash (I have no option on the shell selection) and they both worked.

Could you explain the difference between one set of square brackets vs 2 sets and how come I can use the ">" symbol as "greater than" in bash when using 2 sets of parentheses?

Thanks so much.
# 14  
Old 02-21-2009
As far as the bash shell is concerned, you have:

1. The [ and the test builtin commands. In this context (after the [ and the test command) parameter expansion, word splitting and pathname expansion are performed[1]. So beware of the following situation when using the [ and the test commands:

- missing quotes
-- unset or null values:
Code:
$ unset var
$ [ $var -eq 1 ]
bash: [: -eq: unary operator expected

-- values containing IFS characters:

Code:
$ var="a b"
$ [ $var = "a b" ]
bash: [: too many arguments

-- pathname expansion:

Code:
$ touch x
$ var=\*
$ set -x
$ [ $var = "*" ]
+ '[' x = '*' ']'

- particular syntax for logical comparison:

Code:
$ [ value = value -a 1 -lt 2 ]&&echo true
true

2. The [[ non standard (ksh93, zsh and bash) compound command. Parameter expansion is performed inside [[ ]].
No word splitting and pathname expansion are performed[2]. Support more operators.
Consider the following:

- no need to quote unset variables or variables with null values or values containing IFS characters:

Code:
$ unset var
$ set -x              
$ [[ $var -eq value ]]
+ [[ '' -eq value ]]

Code:
$ var=a\ b
$ set -x
[[ $var == "a b" ]]
+ [[ a b == \a\ \b ]]

- new versions of bash support new operators:

Code:
$ var=abc              
[[ $var =~ ^a ]]&&echo OK
OK
$ echo "${BASH_REMATCH[@]}"
a

- readable operators for logical comparison:

Code:
$ [[ value == value && 2 < 1 ]]||echo false
false

3. The compound command ((expression)). From the manual pages for bash[3]:

Quote:
The expression is evaluated according to the rules described below under ARITHMETIC
EVALUATION. If the value of the expression is non-zero, the return status is 0; oth-
erwise the return status is 1. This is exactly equivalent to let "expression".
Code:
$ unset a
$ ((++a))&&echo $a # version >= 3
1

And as far as your particular problem is concerned it seems like a limitation of the old version of bash you're using,
your code works with recent versions of the bash shell.

[1] For more details see the manual pages for bash, the section Shell Builtin Commands and CONDITIONAL EXPRESSIONS.
[2] For more details see the manual pages for bash, the section Compound Commands. Tilde expansion, arithmetic expansion, command substitution, process substitution, and quote removal are also performed.
[3] For more information see:

Code:
man bash|less -p^ARITHMETIC\ EVALUATION



Hope this helps.

Last edited by radoulov; 02-21-2009 at 03:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing Integers (I think)

Hi, I can't figure out what I'm missing. I'm running a query to see if there are any streams recording on my DVR before starting a scripted update. I'm guessing that it is viewing $VIDEO as a string instead of an int. I've tried everything I saw on google but it still comes back as $VIDEO is... (8 Replies)
Discussion started by: Rhysers
8 Replies

2. Shell Programming and Scripting

Bash - Comparing 2 xml strings masking certain fields

Would like to compare 2 XML Strings which has certain known fields changed. For example, Date field will always have differences. When comparing both strings, skip/mask all the occurring Date Field's `DtField1` and `DtField2` Note: these are not formatted xml format. File1: ... (1 Reply)
Discussion started by: Sajjadmehdi
1 Replies

3. Shell Programming and Scripting

Bash Integers/String

Hy friends, I am newbie to bash scripting, can anyone explain how b=${a/23/BB} # Substitute "BB" for "23". this line converts "b" into string and and "d" into Integer. Thanks in advance (4 Replies)
Discussion started by: Qazi
4 Replies

4. Shell Programming and Scripting

Grep float/integers but skip some integers

Hi, I am working in bash in Mac OSX, I have following 'input.txt' file: <INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined <INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651 <INFO> HypoTestTool: expected limit (median) 11 <INFO> HypoTestTool: ... (13 Replies)
Discussion started by: Asif Siddique
13 Replies

5. Shell Programming and Scripting

Comparing time is bash

Hi, I have a question on comparing time using bash. Tried searching a lot up but couldn't figure it out. So I have this: CURRENT_TIME=$(date +%H:%M) if then echo "Continue" else echo "Quit" fi I tried a lot of different combinations of comparing, but nothing seems to work. Any help... (2 Replies)
Discussion started by: r4v3n
2 Replies

6. Shell Programming and Scripting

Comparing sizes in percentages of 2 files in bash

Hi guys, I hope you can enlight me with a script I'm doing for Solaris 10. Script goes like this: #!/usr/bin/bash fechahoy=`perl /export/home/info/John/fechamod.pl` fechayer=`perl /export/home/info/John/fecha.pl` echo $fechahoy echo $fechayer DAT1=`ssh ivt@blahblah ls -la... (1 Reply)
Discussion started by: sr00t
1 Replies

7. Shell Programming and Scripting

Editing lists of integers in 1d files with bash shell

Hi, I need a script that will: 1. Go through about 20 different folders, each containing about 20 1d files. The 1d files go something like this: 22.253 37.707 78.117 112.374 127.944 156.067 180.956 233.785 249.256 ... (1 Reply)
Discussion started by: ac130pilot
1 Replies

8. Shell Programming and Scripting

Help with comparing 2 lines in 2 different file in shell bash

Hi guys i need help with comparing lines in 2 separate files. Both files contain the same amount of lines and i need to output the difference into the 2nd file. The 1st file is always correct. 1st file (Expected.e): Tuesday, 11 August 2009 Wednesday, 12 August 2009 Thursday, 13 August 2009... (2 Replies)
Discussion started by: kcrap
2 Replies

9. Shell Programming and Scripting

comparing two arrays or strings in bash

Hi there, im having issue with comparing two variables, in a bash script. im trying to do the following: - get a word from user 1 - split the word into array - get a character from user2 trying to compare the character entered by user 2 with every single character in the array... (2 Replies)
Discussion started by: new2Linux
2 Replies

10. UNIX for Dummies Questions & Answers

Bash comparing date

Cound anyone help me on how to compare date in Unix using if function on bash file? current=date if ###syntax is wrong, could anyone correct it for me then rm -rf /usr/local/src fi Thank You... (17 Replies)
Discussion started by: Stanford Co
17 Replies
Login or Register to Ask a Question