Below part of script, is working fine sometimes and gives error sometime.
I am doing float operations, checking if x > y.
##########CODE ##########
THRESHOLD="1.25"
ratio=$( echo "scale=2; ${prev}/${current}" | bc )
if [ $(echo "${ratio} > ${THRESHOLD}" | bc ) -eq 1 ]; then
split_date=`echo ${line} | cut -d, -f2`
fi
########################
below is the error i am getting -
script/split_checks.sh: line 62: [: -eq: unary operator expected
(standard_in) 2: parse error
(standard_in) 1: parse error
Thanks,
Manish
---------- Post updated at 10:47 AM ---------- Previous update was at 10:41 AM ----------
i have put checks to make sure that prev & current are non-zero values....
change
if [ $(echo "${ratio} > ${THRESHOLD}" | bc ) -eq 1 ]; the
to
if [ ${ratio} > ${THRESHOLD} ]; then
agn
August 4, 2010, 1:30am
3
Instead of
if [ $(echo "${ratio} > ${THRESHOLD}" | bc ) -eq 1 ]; then
try
if [ $ratio -le $THRESHOLD ]; then
The reason your shell is complaining is because '$(echo "${ratio} > ${THRESHOLD}" | bc' is throwing an error.
pravin and agn - looks like what you guys said is not helping
#################### CODE ####################
#!/bin/sh
a="1.23"
b="2.45"
if [ $a > $b ]; then
echo "string compare: greater : $a > $b"
else
echo "string compare: smaller : $a < $b"
fi
if [ $a -ge $b ]; then
echo "int compare: greater : $a > $b"
else
echo "int compare: smaller : $a < $b"
fi
if [ $( echo "$a > $b" | bc ) -eq 1 ]; then
echo "using bc: greater : $a > $b"
else
echo "using bc: smaller : $a < $b"
fi
exit 0
#########################################
and output is
$ ./test.sh
string compare: greater : 1.23 > 2.45
./test.sh: line 12: [: 1.23: integer expression expected
int compare: smaller : 1.23 < 2.45
using bc: smaller : 1.23 < 2.45
the first greater is wrong and second (-le/-ge) is giving error
---------- Post updated at 11:05 AM ---------- Previous update was at 11:05 AM ----------
i want to know the reason for this error:
(standard_in) 2: parse error
(standard_in) 1: parse error
shell error will go if "bc" works fine.
agn
August 4, 2010, 1:54am
5
Ah, floating point. Plain shell operators can't handle floating point math. Use perl or awk.
whats wrong with this line:
if [ $(echo "${ratio} > ${THRESHOLD}" | bc ) -eq 1 ]; then
why is it giving error:
(standard_in) 2: parse error
(standard_in) 1: parse error
---------- Post updated at 11:43 AM ---------- Previous update was at 11:42 AM ----------
it should give error only in case one of the values is blank... but i have added enough checks to make sure that they should have atleast 0 value
agn
August 4, 2010, 2:34am
7
bc is probably throwing an error. On my system
$ echo '1 > 2' | bc
syntax error on line 1, teletype
$ echo '1 < 2' | bc
1
$echo '1 > 2' | bc
0
agn
August 4, 2010, 4:48am
9
Try printing
$(echo "${ratio} > ${THRESHOLD}" | bc )
through your script.
THRESHOLD="1.25"
ratio=$( echo "scale=2; ${prev}/${current}" | bc )
if [ $(echo "${ratio} > ${THRESHOLD}" | bc ) -eq 1 ]; then
split_date=`echo ${line} | cut -d, -f2`
fi
got the error.. i was looking at if statement for the error, but the error was on line before that... the problem was - prev/current were non numeric values - which is causing problem with BC...