Scripting >> Examples >> Python-Perl-Powershell-Bash-Windows Batch >> Example 006 - Simple Integer arithmetic and calling a function with parameters


To perform simple integer arithmetic.


Examples in Python, Perl, Powershell, Bash and Windows batch.
 

 

 

 

 

 

 

 

 

 

 

 

 

## NOTE
## 1. For Powershell,
##    (a) To allow hyphen (-) to be used as a script parameter e.g. to run yourscript.ps1 4 - 2
##          use        : powershell yourscript.ps1 4 - 2
##          or         : powershell -Command yourscript.ps1 4 - 2
##          instead of : powershell -File    yourscript.ps1 4 - 2
##    (b) To use ^ as the operator parameter enclose in double quotes e.g.
##          yourscript.ps1 4 "^" 2
##    (c) If you want to associate .ps1 files to be executed by powershell.exe, Open Windows command prompt as administrator
##          assoc .ps1=Microsoft.PowerShellScript.1
##          and either:-
##          ftype Microsoft.PowerShellScript.1=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe "%1" %*
##          or
##          ftype Microsoft.PowerShellScript.1=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -Command "%1" %*
##  Below is a comparison of running the the script with powershell.exe and passing hyphen (-) as one of the parameters


## 2. For Bash,
##    (a) When passing arguments to the script for multiplication using * as the operator symbol, bash may
##        perform globbing of * to match all files on the current working directory.  To avoid this, we use
##
##        set -o noglob
##

## 3. For Windows Batch
##    (a) Cannot use ^ to represent exponentiation operation as it is the escape character for windows, so we used **
##    (b) There is no built in exponentiation / power-of arithmetic operation in Windows Command line, so we emulate
##        with a for loop
##    Online Ref: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands

 

Sample output - Python

 

Sample output - Perl

 

 

Sample output - Powershell

 

Sample output - Bash

Sample output - Windows batch