error while doing decimal comparision in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting error while doing decimal comparision in shell
# 1  
Old 11-12-2008
error while doing decimal comparision in shell

a=10
b=10.6
c=$(echo "$a - $b" | bc)
if [[ "$c" -ge 0 ]]
echo "success"
else
echo "failure"
fi



while executing the above sample code, am getting the below error:

[[: -.6: syntax error: operand expected (error token is ".6")


seems unix is comparing .6 with 0 instead of 0.6 with 0.

can anyone help me in solving this ?

regards,
Chandra
# 2  
Old 11-12-2008
you are missing a "then"

if [[ "$c" -ge 0 ]]
then
....


and put

#!/usr/bin/ksh

on the top of it, your synthax is not working in sh or bash, because with -ge they suppose integer values

Code:
#!/usr/bin/ksh
a=10
b=10.6
c=$(echo "$a - $b" | bc)
if [[ "$c" -ge 0 ]]
then
echo "success"
else
echo "failure"
fi

# 3  
Old 11-12-2008
my mistake....am using "then"......forgot to type that.

a=10
b=10.6
c=$(echo "$a - $b" | bc)
if [[ "$c" -ge 0 ]] ; then
echo "success"
else
echo "failure"
fi
# 4  
Old 11-12-2008
and still am getting the same old error....as mentioned in mi first post.
# 5  
Old 11-12-2008
have you tried it with ksh as I mentioned?
# 6  
Old 11-12-2008
You can try it in Ksh and sh.

It will work when adding "then" in your first post script.

Try this
# 7  
Old 11-12-2008
Need some variable values

Can you get me the value for two variables in your server. So that it's easier to identify the issue.

cat /usr/include/sys/limits.h

and get me the value for

BC_BASE_MAX

BC_SCALE_MAX
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ignoring decimal in comparision

HI All, I am having a requirement on ignoring mismatch on 2 file File 1: A,B,US,10.02 A,B,US,10.02 A,B,US,11.02 File 2: A,B,US,10.02 A,B,US,10.00 A,B,US,12.02 Here I want to ignore the decimal . If I do diff it is showing File1 AND File2 are different.If I ignore the... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

2. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

3. Shell Programming and Scripting

Shell arithmetic : operations on decimal points

i am having a varialbe a , which is input to my file i want to multiply this input with value .43, and assign it to variable b. i tried it as below: #!/bin/sh a=$1 b=`expr $1\*0.43` echo b=$b error : expr: non-integer argument Please tell me , how to do this. Thanks (10 Replies)
Discussion started by: rishifrnds
10 Replies

4. UNIX and Linux Applications

Unix Shell Scripting : Comparision of two files

Hi, We need to compare a text file File1.txt and config file File2.txt in a way that it checks if the content of File1.txt exists between the range mentioned in File2.cfg. The range here is the range between col1 and col2 of File2.cfg If the content of File1.txt lies between the range of... (12 Replies)
Discussion started by: CFA
12 Replies

5. Shell Programming and Scripting

Array comparision in bash shell

I'm not sure if i can put the problem in understandable form.Let me try: I have a array which is like and always fixed: Array1=(new inprogress pending Restored Resolved ) Other 2 array array2 which may varry but always subset of above array: Like it can be : Array2=(New inprogress... (2 Replies)
Discussion started by: InduInduIndu
2 Replies

6. Shell Programming and Scripting

how to do decimal arithmetic in shell script

hi, I have a file with decimal/non-decimal values $ cat b22 373 164 92 62 20 131 94 12 129 111 95 154 37 15 447 25 7.4 135 77 122 32 92 70 57 37 42 72 17 13 97 40 41 53 22 80 71 29 87 23 31 273 6.2 12K 43 44 45 22 11 7.7 13 18 173 36 20 18 13 56 67 104 53 5.4 241 19 13 3.8 38 14 31 329 16 155... (8 Replies)
Discussion started by: sam05121988
8 Replies

7. Shell Programming and Scripting

get the perl version (decimal value comparision)

Hi All, can you pls throw some light for below logic -> Check the perl version -> if the version is greater than or equal to 5.8 -> proceed to next step -> else fail Regards Kamal (2 Replies)
Discussion started by: kamauv234
2 Replies

8. UNIX for Dummies Questions & Answers

File comparision and modification using shell script

Hello everyone, I would like to know how to compare two files and modify any differences with some other data using shell script. I think it would be better understood with an example. I got two files named 'filex' and filey'. 'filex' is constant file without any changes in data. 'filey' is... (2 Replies)
Discussion started by: maddy81
2 Replies

9. Shell Programming and Scripting

String comparision in shell scripting

Hi Guys, I am new to scripting I have written a code to compare strings,but I am getting some Exception Code snippet: MODE="D" if ]; then . $file1 fi Error: ./BatchJobs.sh: [[: execute permission denied I have given all Execute permissions to the script(chmod 755... (2 Replies)
Discussion started by: Anji
2 Replies

10. Shell Programming and Scripting

File Comparision by using Shell Script

Hello All, I am trying to find 2 file comparision by using Shell Script. For example, I am having 2 directories namely DAY1 & DAY2. DAY1 directory contains file1.dat, file2.dat, file3.dat, file4.dat, file5.dat & DAY2 directory contains file1.dat, file2.dat, file3.dat, file4.dat, file5.dat. Now,... (3 Replies)
Discussion started by: nvkuriseti
3 Replies
Login or Register to Ask a Question