0

Bash shell scripting

var1=126
var2=16 
var1/var2 = 7.87

My requirement is if the output value comes in decimal, then add 1 to the integer i.e in this case 7+1 =8. How can I do that?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 1
    It seems you are looking for the ceiling function in `bash`. Here are hints how to do so: (http://stackoverflow.com/questions/2394988/get-ceiling-integer-from-number-in-linux-bash)[http://stackoverflow.com/questions/2394988/get-ceiling-integer-from-number-in-linux-bash] People use `awk` or they invoke a script. – peter_the_oak Mar 01 '15 at 08:34
  • link is dead, unable to view – user3742796 Mar 01 '15 at 08:36
  • Typos, typos, typos.... [Question is here](http://stackoverflow.com/questions/2394988/get-ceiling-integer-from-number-in-linux-bash) – peter_the_oak Mar 01 '15 at 08:39
  • that will do for me, thanks – user3742796 Mar 01 '15 at 08:46

1 Answers1

1

You can use the modulo operator %:

#! /bin/bash
var1=126
var2=16
(( result = var1 / var2 ))
(( var1 % var2 && ++ result )) # If there's a remainder, add 1 to result.
echo $result
choroba
  • 231,213
  • 25
  • 204
  • 289