Sunday, October 24, 2010

Addition, multiplication etc in bash script?

========
#!/bin/bash
x=5 # initialize x to 5
y=3 # initialize y to 3

add=$(($x + $y)) # add the values of x and y and assign it to variable add
sub=$(($x - $y)) # subtract the values of x and y and assign it to variable sub
mul=$(($x * $y)) # multiply the values of x and y and assign it to variable mul
div=$(($x / $y)) # divide the values of x and y and assign it to variable div
mod=$(($x % $y)) # get the remainder of x / y and assign it to variable mod
========

try :)

2 comments:

  1. Excellent blog :

    ---
    http://unstableme.blogspot.com/2009/01/insert-append-change-lines-using-sed.html
    ---

    ReplyDelete
  2. What do you do if x is smaller than y?
    When I try instead of getting a decimal I get zero

    ReplyDelete