| 1 | #!/bin/bash |
| 2 | |
| 3 | echo |
| 4 | echo "String operations using \"expr \$string : \" construct" |
| 5 | echo "===================================================" |
| 6 | echo |
| 7 | |
| 8 | a=1234zipper5FLIPPER43231 |
| 9 | |
| 10 | echo "The string being operated upon is \"`expr "$a" : '\(.*\)'`\"." |
| 11 | # Escaped parentheses grouping operator. == == |
| 12 | |
| 13 | # *************************** |
| 14 | #+ Escaped parentheses |
| 15 | #+ match a substring |
| 16 | # *************************** |
| 17 | |
| 18 | |
| 19 | # If no escaped parentheses ... |
| 20 | #+ then 'expr' converts the string operand to an integer. |
| 21 | |
| 22 | echo "Length of \"$a\" is `expr "$a" : '.*'`." # Length of string |
| 23 | |
| 24 | echo "Number of digits at the beginning of \"$a\" is `expr "$a" : '[0-9]*'`." |
| 25 | |
| 26 | # ------------------------------------------------------------------------- # |
| 27 | |
| 28 | echo |
| 29 | |
| 30 | echo "The digits at the beginning of \"$a\" are `expr "$a" : '\([0-9]*\)'`." |
| 31 | # == == |
| 32 | echo "The first 7 characters of \"$a\" are `expr "$a" : '\(.......\)'`." |
| 33 | # ===== == == |
| 34 | # Again, escaped parentheses force a substring match. |
| 35 | # |
| 36 | echo "The last 7 characters of \"$a\" are `expr "$a" : '.*\(.......\)'`." |
| 37 | # ==== end of string operator ^^ |
| 38 | # (In fact, means skip over one or more of any characters until specified |
| 39 | #+ substring found.) |
| 40 | |
| 41 | echo |
| 42 | |
| 43 | exit 0 |