
146
|
Chapter 3: Linux Commands
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
substr string start length
Return a section of string, beginning with start, with a
maximum length of length characters. Return null when given
a negative or nonnumeric start or length.
Examples
Division happens first; result is 10:
expr 5 + 10 / 2
Addition happens first; result is 7 (truncated from 7.5):
expr \( 5 + 10 \) / 2
Add 1 to variable i. This is how variables are incremented in shell
scripts:
i=`expr $i + 1`
Print 1 (true) if variable a is the string “hello”:
expr $a = hello
Print 1 (true) if b plus 5 equals 10 or more:
expr $b + 5 \>= 10
Find the 5th, 6th, and 7th letters of the word character:
expr substr character 5 3
In the examples that follow, variable p is the string “version.100”.
This command prints the number of characters in p:
expr $p : '.*' Result is 11
Match all characters and print them:
expr $p : '\(.*\)' Result is "version.100"
Print the number of lowercase letters at the beginning of p:
expr $p : '[a-z]*' Result is 7
Match the lowercase letters at the beginning of p:
expr $p : '\([a-z]*\)' Result is "version"
Truncate $x if it contains five or more characters; if not, just print
$x. (Logical OR uses the second argument when the first one is 0
or null, i.e., when the match fails.)
expr $x : '\(.....\)' \| $x
In a shell script, rename files to ...