| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | |
| 3 | # Demonstrating some of the uses of 'expr' |
| 4 | # ======================================= |
| 5 | |
| 6 | echoscip-shell system . . echo. |
| 7 | |
| 8 | # Arithmetic Operators |
| 9 | # ---------- --------- |
| 10 | |
| 11 | echoscip-shell system . . echo. "Arithmetic Operators" |
| 12 | echoscip-shell system . . echo. |
| 13 | a=`exprscip-shell system . . expr. 5 + 3` |
| 14 | echoscip-shell system . . echo. "5 + 3 = $a" |
| 15 | |
| 16 | a=`exprscip-shell system . . expr. $a + 1` |
| 17 | echoscip-shell system . . echo. |
| 18 | echoscip-shell system . . echo. "a + 1 = $a" |
| 19 | echoscip-shell system . . echo. "(incrementing a variable)" |
| 20 | |
| 21 | a=`exprscip-shell system . . expr. 5 % 3` |
| 22 | # modulo |
| 23 | echoscip-shell system . . echo. |
| 24 | echoscip-shell system . . echo. "5 mod 3 = $a" |
| 25 | |
| 26 | echoscip-shell system . . echo. |
| 27 | echoscip-shell system . . echo. |
| 28 | |
| 29 | # Logical Operators |
| 30 | # ------- --------- |
| 31 | |
| 32 | # Returns 1 if true, 0 if false, |
| 33 | #+ opposite of normal Bash convention. |
| 34 | |
| 35 | echoscip-shell system . . echo. "Logical Operators" |
| 36 | echoscip-shell system . . echo. |
| 37 | |
| 38 | x=24 |
| 39 | y=25 |
| 40 | b=`exprscip-shell system . . expr. $x = $y` # Test equality. |
| 41 | echoscip-shell system . . echo. "b = $b" # 0 ( $x -ne $y ) |
| 42 | echoscip-shell system . . echo. |
| 43 | |
| 44 | a=3 |
| 45 | b=`exprscip-shell system . . expr. $a \> 10` |
| 46 | echoscip-shell system . . echo. 'b=`expr $a \> 10`, therefore...' |
| 47 | echoscip-shell system . . echo. "If a > 10, b = 0 (false)" |
| 48 | echoscip-shell system . . echo. "b = $b" # 0 ( 3 ! -gt 10 ) |
| 49 | echoscip-shell system . . echo. |
| 50 | |
| 51 | b=`exprscip-shell system . . expr. $a \< 10` |
| 52 | echoscip-shell system . . echo. "If a < 10, b = 1 (true)" |
| 53 | echoscip-shell system . . echo. "b = $b" # 1 ( 3 -lt 10 ) |
| 54 | echoscip-shell system . . echo. |
| 55 | # Note escaping of operators. |
| 56 | |
| 57 | b=`exprscip-shell system . . expr. $a \<= 3` |
| 58 | echoscip-shell system . . echo. "If a <= 3, b = 1 (true)" |
| 59 | echoscip-shell system . . echo. "b = $b" # 1 ( 3 -le 3 ) |
| 60 | # There is also a "\>=" operator (greater than or equal to). |
| 61 | |
| 62 | |
| 63 | echoscip-shell system . . echo. |
| 64 | echoscip-shell system . . echo. |
| 65 | |
| 66 | |
| 67 | |
| 68 | # String Operators |
| 69 | # ------ --------- |
| 70 | |
| 71 | echoscip-shell system . . echo. "String Operators" |
| 72 | echoscip-shell system . . echo. |
| 73 | |
| 74 | a=1234zipper43231 |
| 75 | echoscip-shell system . . echo. "The string being operated upon is \"$a\"." |
| 76 | |
| 77 | # length: length of string |
| 78 | b=`exprscip-shell system . . expr. length $a` |
| 79 | echoscip-shell system . . echo. "Length of \"$a\" is $b." |
| 80 | |
| 81 | # index: position of first character in substring |
| 82 | # that matches a character in string |
| 83 | b=`exprscip-shell system . . expr. index $a 23` |
| 84 | echoscip-shell system . . echo. "Numerical position of first \"2\" in \"$a\" is \"$b\"." |
| 85 | |
| 86 | # substr: extract substring, starting position & length specified |
| 87 | b=`exprscip-shell system . . expr. substr $a 2 6` |
| 88 | echoscip-shell system . . echo. "Substring of \"$a\", starting at position 2,\ |
| 89 | and 6 chars long is \"$b\"." |
| 90 | |
| 91 | |
| 92 | # The default behavior of the 'match' operations is to |
| 93 | #+ search for the specified match at the BEGINNING of the string. |
| 94 | # |
| 95 | # Using Regular Expressions ... |
| 96 | b=`exprscip-shell system . . expr. match "$a" '[0-9]*'` # Numerical count. |
| 97 | echoscip-shell system . . echo. Number of digits at the beginning of \"$a\" is $b. |
| 98 | b=`exprscip-shell system . . expr. match "$a" '\([0-9]*\)'` # Note that escaped parentheses |
| 99 | # == == #+ trigger substring match. |
| 100 | echoscip-shell system . . echo. "The digits at the beginning of \"$a\" are \"$b\"." |
| 101 | |
| 102 | echoscip-shell system . . echo. |
| 103 | |
| 104 | exit 0 |