2001-09-04 13:36:09 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
echo
|
2002-06-03 14:35:48 +00:00
|
|
|
echo "String operations using \"expr \$string : \" construct"
|
|
|
|
echo "==================================================="
|
2001-09-04 13:36:09 +00:00
|
|
|
echo
|
|
|
|
|
2002-06-03 14:35:48 +00:00
|
|
|
a=1234zipper5FLIPPER43231
|
|
|
|
|
2001-09-04 13:36:09 +00:00
|
|
|
echo "The string being operated upon is \"`expr "$a" : '\(.*\)'`\"."
|
2002-06-03 14:35:48 +00:00
|
|
|
# Escaped parentheses grouping operator. == ==
|
|
|
|
|
|
|
|
# ***************************
|
|
|
|
#+ Escaped parentheses
|
|
|
|
#+ match a substring
|
|
|
|
# ***************************
|
|
|
|
|
|
|
|
|
2012-11-27 14:56:18 +00:00
|
|
|
# If no escaped parentheses ...
|
2002-06-03 14:35:48 +00:00
|
|
|
#+ then 'expr' converts the string operand to an integer.
|
2001-09-04 13:36:09 +00:00
|
|
|
|
|
|
|
echo "Length of \"$a\" is `expr "$a" : '.*'`." # Length of string
|
|
|
|
|
|
|
|
echo "Number of digits at the beginning of \"$a\" is `expr "$a" : '[0-9]*'`."
|
|
|
|
|
2002-06-03 14:35:48 +00:00
|
|
|
# ------------------------------------------------------------------------- #
|
|
|
|
|
|
|
|
echo
|
|
|
|
|
2001-09-04 13:36:09 +00:00
|
|
|
echo "The digits at the beginning of \"$a\" are `expr "$a" : '\([0-9]*\)'`."
|
2002-06-03 14:35:48 +00:00
|
|
|
# == ==
|
|
|
|
echo "The first 7 characters of \"$a\" are `expr "$a" : '\(.......\)'`."
|
|
|
|
# ===== == ==
|
|
|
|
# Again, escaped parentheses force a substring match.
|
|
|
|
#
|
|
|
|
echo "The last 7 characters of \"$a\" are `expr "$a" : '.*\(.......\)'`."
|
|
|
|
# ==== end of string operator ^^
|
2012-11-27 14:56:18 +00:00
|
|
|
# (In fact, means skip over one or more of any characters until specified
|
|
|
|
#+ substring found.)
|
2001-09-04 13:36:09 +00:00
|
|
|
|
|
|
|
echo
|
|
|
|
|
|
|
|
exit 0
|