The main purpose of the zsh shell was to provide an advanced programming environment forshell programmers. With that in mind, it’s no surprise that the zsh shell offers many features that make shell scripting easier. Mathematical operations
As you would expect, the zsh shell allows you to perform mathematical functions with ease. Similarto the ksh93 shell, the zsh shell has full support for floating-point numberin all of its mathematical operations. Performing calculations The zsh shell supports the same two methods for performing mathematical operations as theksh93 shell:
When you use the let command you should enclose the operation in double quotation marks to allow for spaces:% let value1=" 4 * 5.1 / 3.2 "
Notice that using floating point numbers introduces a precision problem. To solve this, it’s alwaysa good idea to use the printf command, and specify the decimal precision needed to correctlydisplay the answer:
The second method is to use the double parentheses. This method incorporates two techniques fordefining the mathematical operation:
Notice that you can place the double parentheses either around just the operation (preceded by adollar sign) or around the entire assignment statement. Both methods produce the same results.
If you don’t use the typeset command to declare the data type of a variable beforehand, the zshshell attempts to automatically assign the data type. This can be dangerous when working with both integer and floating-point numbers. Take a look at this example:% value=10
Now that’s probably not the answer you want to come out from the calculation. When you specify numberswithout decimal places, the zsh shell interprets them as integer values, and performsinteger calculations. To ensure that the result is a floating point number, you must specify the numbers with decimal places:% value=10.
Now the result is in the floating point format.Mathematical functions With the zsh shell, built-in mathematical functions are either feast or famine. The default zsh shell doesn’t include any special mathematical function.However, if you install the zsh/mathfuncmodule, you’ll have more math functions than you’ll most likely ever need:% value1=$(( sqrt(9) ))
intf "%6.3f " $value1 6.375 % Now that’s much better!
This also applies to shell scripts. If you have a function you need to use, you’ll need to use the auto load command to make sure that it’s available:% cat test4
Another interesting feature of the zsh shell is the zcompile command. This command processes a function file and creates a ‘‘compiled’’ version for theshe .This isn’t really the same type ofcompiling you’re used to in other programming languages. It does, however, put the function into abinary format that the zsh shell can load more quickly.
When you run the zcompile command, it creates a .zwc version of the function file. When theautoload command looks for the command in the fpath, it’ll see the .zwc version and load it instead of the text function file.
Shell Scripting Related Interview Questions
|Perl Scripting Interview Questions||Python Interview Questions|
|Linux Interview Questions||Linux Embedded systems Interview Questions|
|AWK Interview Questions||BioPerl Interview Questions|
|Sed (Stream Editor) Interview Questions||Advanced Linux Interview Questions|
|Unix/Linux Interview Questions||Unix Shell Scripting Interview Questions|
Shell Scripting Tutorial
Starting With Linux Shells
Getting To The Shell
Basic Bash Shell Commands
More Bash Shell Commands
Using Linux Environment Variables
Basic Script Building
Understanding Linux File Permissions
Working With Editors
Using Structured Commands
More Structured Commands
Handling User Input
Adding Color To Scripts
Introducing Sed And Gawk
The Ash Shell
The Tcsh Shell
The Korn Shell
The Zsh Shell
Using The Web
Using A Database
Shell Script For Administrators
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.