bash scripting arithmetic operations. objectives reinforce basic scripting: – variables – i/o...
TRANSCRIPT
BASH Scripting
Arithmetic Operations
Objectives
• Reinforce basic scripting:– Variables– I/O– Execution
• Introduce Arithmetic operations:– integer operations
Overview
• Arithmetic operations– ** (exponentiation)– *,/,% (multiplication, division, remainder)– ++,-- (increment, decrement)– +,- (addition, subtraction)
Arithmetic Operations• Arithmetic Operators (binary)– ** (exponentiation) order is left to right– *,/,% (multiplication, division, remainder)– +,- (addition, subtraction)
• Assignment (op= operations above)– expr var1 op var2 does not include ** op,
spaces required– let var=var1opvar2 # Note no spaces– (( var = var1 op var2)) # space independent– declare [-ix] var=var1opvar2
• Arithmetic Operators (unary)– ++,-- (increment, decrement)
declare
• declare is a bash command that will establish the type of variable and optionally give it an initial value.
• Format: declare [options] var[={val|expr}]• Selected options:
Option Meaning
-a declare an array , bash has no limit to size of array, subscripts start at zero(0)(see page 364)
-i declare an integer
-r declare read only
-x declare variable for export
declare examplesCode Explanation
declare –i varvar=2*7
var is declared to be an integer, thus the let is not required.
declare var=3*2 Since the –i is missing var is set to “3*2”, not 6
declare –ix var=6-2 var is declared to be an exported integer with an initial value of 6-2 (4)
declare –ia arrarr[0]=3arr[1]=2arr[2]=${arr[0]}*${arr[1]}
An array of integers (-ia) is declaredarr first location is set to the integer 3arr second location is set to the integer 2arr third location is set to the result of multiplying arr[0] by arr[1], result is 6 is placed in arr[2]
Note the square brackets [] are part of the subscript syntax and do not mean optional.
let, declare, and assignmentCommand Example Description
let let val=3+4 Add 3 to 4 and puts the result (7) in val
declare declare -ix val=3*4 creates an integer data type (-i) variable called val, that also will be exported (-x) and assigns it the value 12.
= val=3*4 puts the string “3*4” into val, unless val has been declared to be an integer (e.g. declare –I val)
Sample of Arithmetic/AssignmentStatements
• Integer– expr 3 / 5– expr 3 \* 5– expr 3 ** 5 # INVALID!!!!– let val=3*4 # see caution below!– (( val = 3 * 5 ))– (( val = 3 ** 5 ))
Do NOT use val=3*4 to do arithmetic, you must use the let command (unless val has been declared to be an integer).
Sample scripts
• comp – shows declare and (( )) operatorsdeclare -i testval=20declare -i count=2(( result = $testval * $count ))echo $result
Array Example
array=(red green blue yellow magenta)len=${#array[*]}echo "The array has $len members. They are:"i=0while [ $i -lt $len ]; do
echo "$i: ${array[$i]}"let i++
done
Results of Running Script
The array has 5 members. They are:0: red1: green2: blue3: yellow4: magenta
Lucky Numbers Assignment
Accept birthday (1-31) and birth month (1-12) from the user. Use that information to generate 6 random numbers between 1 and 45 to represent lucky lotto numbers. Your six numbers need to be stored in an array. The array will have indices 0-5. You will
use arithmetic manipulations of the birth day and month to generate the lucky numbers. Everyone with the same birthday and month will get the same lucky numbers – everyone with a different birthday and month will get different lucky numbers.
There can be no duplicates in the lucky numbers.