|
||||||
Bash is the default shell for Linux and is very powerful. However, it lacks in-built mathematical functions - something that that the Korn shell does have.
Anyone using the Linux command line or developing scripts will, by default, be using the Bourne-Again shell (Bash); and when using this shell they will be able to carry out simple mathematical calculations, such as: (( a = 3 )) (( b = 9 )) (( c = a * b )) echo $c # output will be 27 However, the Bash script writer will soon find that they will see errors when they try something like: (( d = sqrt(c) )) # an error will occur on this line Bash, therefore needs to make use of some other tools - such as bc: d=`echo "scale = 2;sqrt($c)" | bc` echo $d # output will be 5.19 or awk: d=`echo $c | awk '{print sqrt($1)}'` echo $d #output will be 5.19615 However, if a Linux user wants to be able to simplify their calculations then they must turn to another shell - the Korn shell. Using the Korn ShellThe Bourne-Again shell is set as the default in most (if not all) of the Linux distributions (Debian, Red Hat, Suse, etc, etc); however the Korn shell is normally installed - and it does everything that Bash does plus a little extra - such as built-in mathematics. It's therefore just a matter of telling the PC to use the correct shell; and this can be done from the command line by typing: ksh or in a shell script by setting the fist line to be: #!/bin/ksh Korn Shell Mathematical FunctionsWhen using the Korn shell the following functions are available to the user:
Examples of Using the Korn Shell Mathematical FunctionsThe most obvious example is the code at the start of this article, and so a complete Korn shell script to calculate the multiplication and square root would be: #!/bin/ksh (( a = 3 )) (( b = 9 )) (( c = a * b )) (( d = sqrt(c) )) echo $d All of the functions have the same basic format: (( result = function(input) )) However, there are a few functions that may give unexpected results and those are the trigonometric and inverse trigonometric functions. The confusion can be caused by the fact that they expect to be working with radians rather than degrees. Of course, with that knowledge the Korn script writer will have no more problems: (( pi = acos(-1) )) # A simple way of calculating pi (( d = 30 )) #Start with 30 degrees (( r = d * pi / 180 )) #Convert degrees to radians (( c = cos(r) )) # Find cosine (( r2 = acos(c) )) # Get arccosine (( d2 = r2 * 180 / pi )) #Convert back to degrees echo $d2 # Back to original number i.e. 30 degrees ConclusionFor most Linux command line tasks or shell scripting the default Bourne-Again shell (Bash) does the job beautifully. However, when it's few limitations are encountered then the Korn shell is a great substitute and is a powerful addition to the shell programmer's set of tools.
The copyright of the article Mathmatical Functions and Linux Shells in Command Line Programming is owned by Mark Alexander Bain. Permission to republish Mathmatical Functions and Linux Shells in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||