hey guys welcome to the next blog on shell scripting tutorials for beginners in the last blog we have seen how we can perform basic Electra Matic operations in shell scripting now in this blog we will see how we can perform the same automatic operations on decimal numbers now by default the scripting doesn't support decimal number or thematic operations using these two method which I have shown you in the last blog now we need to use a special tool and there are many tools and I am going to show you one of the tools which are frequently used to perform automatic operations on decimal numbers but first I will show you what the problem with these two method and we want to perform the automatic operation on decimal number so for example instead of 20 I will just take 20 point 5 here and I want to see the result what I get when I perform the these addition or subtraction or multiplication division or modulus operations on these two numbers right so let me run the script and let's see what happens so you can see we got this error that this is invalid the automatic operator and you know you can see these kind of errors here right so when the value is decimal shell scripting of the bash scripting does not perform these are automatic operations on those numbers here also it says non integer argument for using whenever you be used exp our command it says non integer argument right so we need to use a special tool which is called BC BC stands for basic calculator and most of the time it comes with your Linux distribution right so you can just search for BC you can do man BC here and then press ENTER and you will be able to see this kind of man page of BC and by definition you can see BC is an arbitrary precision calculator language so it's a language in itself for basic calculations right now if you want to know more about this command you can read what are the options we can use in with BC or you know what we can do with BC right but for now we want to do the basic arithmetic operations so let me clear this script and in here also we will just remove this and for now what we want to do is some basic arithmetic operations let scripting so how we can perform these operations so what we do is we basically for example what what up with whatever integers or decimal values you want to perform this automatic operations you just need to write those integers for example I want to add twenty point five plus five for example and if I want to do this automatic operation I can use a pipe symbol here and then the BC command okay and this basically means is whatever come on you use on the left hand side we are using this echo and this text on the left hand side will be treated as an input for the command on the right hand side of this pipe okay so this will be treated as an input for the BC command okay and let's see what happens when we run the script once again so let's run the script once again and it gives us the answer right it gives twenty-five point five which is true and we can do the different automatic operations using this PC command so let's perform them so for example I want to do the subtraction
I want to do the multiplication for example and I want to do the division and I want to do the modulus operation and let's run the script once again it does give the answer of each operation so addition gives us the right answer subtraction is also correct you can see here multiplication is also correct division in the case of division you can see it gives a give it's giving us four right and it's not giving us the proper value of division and I will tell you why and I will also tell you how we can solve this problem right but modulus operation is giving us the correct value so we want to solve this problem which is for the division so to solve this problem we need to set a variable scale for this division operation and how we can do it we can just write scale here so just write scale and then we need to set the value of scale for example we want to have the result up to two decimal places then we can do scale is equal to two so scale is the value we want the decimal places to be right so we want the two decimal places then scale can be two you want the 20 wrestle places then the scale can be 20 right and once again we run the script and you can see this time we got the correct result so always use the scale whenever you use the division operation right and division operation or scale is just our decimal point value whatever you want so if I write here 20 decimal points or 20 as a scale and when I run the script you can see I get the result up to 20 decimal points right so this is how you can use BC but BC as you have seen in the main pages it's not just for arithmetic operations it can do much more than that so for example we want to calculate the square root of some number or if you want to do some trigonometric operations with BC you can do everything now we have seen as we have done this arithmetic operations with these decimal values but what if we have these two you know variables and we need to use these variables in place of these values so to do this you can just do like this as you do with variables so variable number one plus dollar num - mum - okay same you can do with other you know other values so instead of this you can just - - here and so on right and let's run the script once again and it gives us the same result so whenever you want to use the variable use the dollar symbol with the variable and the arithmetic operation you want to perform now I was talking about the square root so how we can perform the square root or something so say I have a number once again I will declare a variable num and its value is for example I will say 27 okay and I want to do the square root of 27 so I can just do echo and then first of all I will set the scale to be 2 for example so scale is equal to 2 and then the semicolon and then there is a function called square root or sq RT so you just use sq RT and then in the bracket you can use the variable for which you want to calculate the square root so I want to calculate the square of this variable or this number so I can do something like this now after this as you already have guessed we use BC but alone of it BC command it will not work because this is a math operation and to use this math operation we need to use the math library with this BC command and to use this math library you just need to write minus L for that right and minus L for calling the math library in which the square root function is okay and let's run the script once again let me clear the terminal and run the script once again and you can see the last value here is the square root of 27 which is correct you can do the square root of 27 and you will get this result right for example I want to do the square root of 4 and I want to run the script once again you can see the result is 2 which is easier to understand right now for example I want to perform for example I want to get the power of 4 something so something to the power of something how we can get it so for this we just need to use whatever integer I have so for example I want to get 3 to the power 3 I can write 3 and then this symbol I don't know what we call the symbol for example cap symbol and then 3 ok so this means to the power okay so 3 to the power 3 and once again we can run the script and it gives us 27 which is correct 3 to the power 3 is 27 so there are many math operations you can perform the width BC and you can do a man BC for reference so you can give this command and you can go little bit down so you can see for calling the math library which we have used here we use minus L Flags is for interactive mode W is for warning as is for standard q is for quit whenever we run the BC command on terminal this is handy command to quit out of the BC command and then V is for version and then when I scroll a little bit down I can see what are the operation I can do with the BC command so I can do post increment or pre increment you can see here multiplication and subtraction division modular of operation and you can see this is for calculating ^ something right and then we have other operations here also so comparison also you can do with BC commands and all the logical operations you can do and we have called this function or method which is used to calculate the square root which is this one right so you can read this man page and you can see what are the things you can do with BC come on so just to man BC and you will be good to go so I hope you've enjoyed this blog
إرسال تعليق