math with variables in shell script

36 Views Asked by At

So, suppose I have the following, where I create 2 variables in a shell (bash) script that are the output of various commands:

# total number of records in file
tot=$(wc -l < /usr/local/test.dat)
echo "total records is: ${tot}"

# number of those records that contain the word wumpus
den=$(grep -r "wumpus" /usr/local/test.dat | wc -l)
echo "total lines containing wumpus: ${den}" 

Works fine. But, what I want to do next is calculate the proportion of lines in the file containing wumpus. In other words, den/tot, and echo that. I've tried most permutations of things like the following, but no luck.

# calculate proportion
prop=$(${den}/${tot})
echo ${prop}

Am I correct in concluding that 'doing math' on a bash variable that is the output of a command is somehow different than doing math with explicitly declared bash variables? For example, the following works fine

a=2
b=3
echo $(($a+$b))
0

There are 0 best solutions below