Easily find issues by searching: #<Issue ID>
Example: #1832
Easily find members by searching in: <username>, <first name> and <last name>.
Example: Search smith, will return results smith and adamsmith
assignment
variable assignment
Initializing or changing the value of a variable
All-purpose assignment operator, which works for both arithmetic and string assignments.
var=27 category=minerals # No spaces allowed after the "=".
Do not confuse the "=" assignment operator with the = test operator.
# = as a test operator if [ "$string1" = "$string2" ] then command fi # if [ "X$string1" = "X$string2" ] is safer, #+ to prevent an error message should one of the variables be empty. # (The prepended "X" characters cancel out.)
arithmetic operators
plus
minus
multiplication
division
exponentiation
# Bash, version 2.02, introduced the "**" exponentiation operator. let "z=5**3" # 5 * 5 * 5 echo "z = $z" # z = 125
modulo, or mod (returns the remainder of an integer division operation)
bash$ expr 5 % 3 2
5/3 = 1, with remainder 2
This operator finds use in, among other things, generating numbers within a specific range (see Example 9-11 and Example 9-15) and formatting program output (see Example 27-16 and Example A-6). It can even be used to generate prime n umbers, (see Example A-15). Modulo turns up surprisingly often in numerical recipes.
Example 8-1. Greatest common divisor
#!/bin/bash # gcd.sh: greatest common divisor # Uses Euclid's algorithm # The "greatest common divisor" (gcd) of two integers #+ is the largest integer that will divide both, leaving no remainder. # Euclid's algorithm uses successive division. # In each pass, #+ dividend <--- divisor #+ divisor <--- remainder #+ until remainder = 0. # The gcd = dividend, on the final pass. # # For an excellent discussion of Euclid's algorithm, see #+ Jim Loy's site, http://www.jimloy.com/number/euclids.htm. # ------------------------------------------------------ # Argument check ARGS=2 E_BADARGS=85 if [ $# -ne "$ARGS" ] then echo "Usage: `basename $0` first-number second-number" exit $E_BADARGS fi # ------------------------------------------------------ gcd () { dividend=$1 # Arbitrary assignment. divisor=$2 #! It doesn't matter which of the two is larger. # Why not? remainder=1 # If an uninitialized variable is used inside #+ test brackets, an error message results. until [ "$remainder" -eq 0 ] do # ^^^^^^^^^^ Must be previously initialized! let "remainder = $dividend % $divisor" dividend=$divisor # Now repeat with 2 smallest numbers. divisor=$remainder done # Euclid's algorithm } # Last $dividend is the gcd. gcd $1 $2 echo; echo "GCD of $1 and $2 = $dividend"; echo # Exercises : # --------- # 1) Check command-line arguments to make sure they are integers, #+ and exit the script with an appropriate error message if not. # 2) Rewrite the gcd () function to use local variables. exit 0
plus-equal (increment variable by a constant) 1
let "var += 5" results in var being incremented by 5.
minus-equal (decrement variable by a constant)
times-equal (multiply variable by a constant)
let "var *= 4" results in var being multiplied by 4.
slash-equal (divide variable by a constant)
mod-equal (remainder of dividing variable by a constant)
Arithmetic operators often occur in an expr or let expression.
About AquaClusters Privacy Policy Support Version - 19.0.2-4 AquaFold, Inc Copyright © 2007-2017