hey guys welcome to the next blog on a shelf scripting tutorial for beginners in this blog we will learn how to perform arithmetic operations in shell script so let's get started so to perform arithmetic operations you may you know consider using something like this so you can echo and then you can do one plus one but by default echo command takes anything which is written after this echo command as a string so if you run this script you will see it will just dump whatever which is written after the psycho command as it is so it's not performing any automatic operation so how do we achieve it so let's take an example and then we will see how do we do it so I will create two variable num one is equal to 20 for example and number two is equal to five for example okay and then I want to perform some basic mathematics on these integer numbers for example addition subtraction or multiplication division or modulus operation so to perform this you take this number as it is and then for example I want to do addition I can just - num - something like this addition is done like this only but for this you just need to enclose these two variables inside double brackets so you just need to do double bracket this side and double bracket on the other side of this expression like this okay but still you will not be able to achieve your goal so if you do echo still it will print as it is but when you do this dollar symbol or when you add this dollar symbols before your expression this double parentheses expression then let's see what happens and don't forget to give the space here okay and let's run the script and let's see what happens so you can see it has performed the automatic addition on these two variables right so to perform these basic arithmetic operations you need these double to parentheses and then this dollar symbol before them so they could be evaluated as an automatic operation right in a similar way we can perform other automatic operations for example if you want to do subtraction you can do something like this if you want to do multiplication we use asterisk or star whatever you want to call it and then for division you just need to give this forward slash here and there is one more operation which is also considered to be a basic automatic operation which is this modulus operation so let's see what happens so when we run the script one by one we will evaluate all the expressions so what's happening here is in the first echo command we are doing the addition which is 25 which is 20 plus 5 25 which is true then the subtraction which is also true you can see here then the multiple multiplication 20 in x 5 is 100 which is also true and then we have done the division which is also true and then we have done the model large modulus operation so modulus operation means that whatever you get as a remainder after dividing the number that remainder will be you know treated as a result okay in our case there is no remainder when we divide 5 by 20 so we get 0 here but if you use for example 21 here then you will get one as a remainder because five will be divided by 21 when it's divided by 21 we get the remainder of one okay so this is how you perform the basic automatic operations in scripting now there is an alternate way of performing automatic operations on your integers and that is by using expr command so to use exp our command we are going to use the same variable but you use only one parenthesis like this here so just use one parenthesis like this and instead of using double brackets or parenthesis you just need our keyword expr okay so you just need to use expr and then for the variables you just need to use them as a normal variable with a dollar symbol something like this so use this kind of a dollar symbol before every you know variable so I'm going to just use this dollar symbol before every variable and this expr command before these operations something like this okay and let's run the script once again and let's see what happens and you can see it's giving us the result but for the multiplication is giving us the error and this says it's a syntax error because in whenever you are using expr this Asterix is not escaped okay so you need to use this Asterix with this escape character which is backslash and then everything will be okay so once again when you run the script you will see the same result as you got with the expression the double parentheses but don't forget to use this escape character before this asterisk for multiplication whenever you are using this expr command okay so this is how you can do the are automatic operation by using these two ways on integers but what happens when you want to perform these automatic operations on for example decimal values this we are going to see in the next blog when I'm going to show you how we can perform these automatic operations on some decimal values
Post a Comment