Results 1 to 5 of 5
#!/bin/bash
a=19
b=17
s1=$(echo "scale=1;($a/31)*100" | bc -l)
s2=$(echo "scale=1;($b/31)*100" | bc -l)
echo "s1 = $s1 and s2 = $s2"
if [ $s1 -gt $s2 ]; then echo "Holiday"; ...
- 11-22-2008 #1Linux Newbie
- Join Date
- Feb 2007
- Posts
- 248
help required for maths in bash script
#!/bin/bash
a=19
b=17
s1=$(echo "scale=1;($a/31)*100" | bc -l)
s2=$(echo "scale=1;($b/31)*100" | bc -l)
echo "s1 = $s1 and s2 = $s2"
if [ $s1 -gt $s2 ]; then echo "Holiday"; else echo "More work"; fi
output:
s1 = 60.0 and s2 = 50.0
n: line 7: [: 60.0: integer expression expected
More work
and when I tried
s1=$(echo "scale=0;($a/31)*100" | bc -l)
s2=$(echo "scale=0;($b/31)*100" | bc -l)
echo "s1 = $s1 and s2 = $s2"
got the following output
s1 = 0 and s2 = 0
More work
Regards
- 11-22-2008 #2Debian GNU/Linux -- You know you want it.
- 11-22-2008 #3Linux User
- Join Date
- Jun 2007
- Posts
- 318
The only type of numbers that bash can handle are integers. You're trying to compare 2 reals. The decimal point in the numbers caused the error 'integer expression expected'.
You'd have to compare two reals using bc, something like this:
Code:rslt=$(echo "scale=1; if ($s1 > $s2) print 1 else print 0" | bc -l) if [ $rslt = 1 ]; then echo "Holiday"; else echo "More work"; fi
- 11-22-2008 #4Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
If you needed the values in bash, and you were keeping track of the scaling yourself, you could use:
Producing:Code:#!/bin/bash - # @(#) s1 Demonstrate integer arithmetic by arranging operations. echo echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) bc set -o nounset echo echo " Results:" a=19 b=17 # s1=$(echo "scale=0;($a/31)*100" | bc -l) # s2=$(echo "scale=0;($b/31)*100" | bc -l) s1=$(echo "scale=0;($a*100)/31" | bc -l) s2=$(echo "scale=0;($b*100)/31" | bc -l) echo "s1 = $s1 and s2 = $s2" exit 0
cheers, drlCode:% ./s1 (Versions displayed with local utility "version") Linux 2.6.11-x1 GNU bash 2.05b.0 bc 1.06 Results: s1 = 61 and s2 = 54
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 11-24-2008 #5Linux Newbie
- Join Date
- Jul 2008
- Posts
- 181
Since you are already using bc, you might as well use its relational expressions.


Reply With Quote