Revision 21276,
1.1 KB
checked in by zacheiss, 20 years ago
(diff) |
This commit was generated by cvs2svn to compensate for changes in r21275,
which included commits to RCS files with non-trunk default branches.
|
Line | |
---|
1 | # |
---|
2 | # substr -- a function to emulate the ancient ksh builtin |
---|
3 | # |
---|
4 | |
---|
5 | # |
---|
6 | # -l == shortest from left |
---|
7 | # -L == longest from left |
---|
8 | # -r == shortest from right (the default) |
---|
9 | # -R == longest from right |
---|
10 | |
---|
11 | substr() |
---|
12 | { |
---|
13 | local flag pat str |
---|
14 | local usage="usage: substr -lLrR pat string or substr string pat" |
---|
15 | |
---|
16 | case "$1" in |
---|
17 | -l | -L | -r | -R) |
---|
18 | flag="$1" |
---|
19 | pat="$2" |
---|
20 | shift 2 |
---|
21 | ;; |
---|
22 | -*) |
---|
23 | echo "substr: unknown option: $1" |
---|
24 | echo "$usage" |
---|
25 | return 1 |
---|
26 | ;; |
---|
27 | *) |
---|
28 | flag="-r" |
---|
29 | pat="$2" |
---|
30 | ;; |
---|
31 | esac |
---|
32 | |
---|
33 | if [ "$#" -eq 0 ] || [ "$#" -gt 2 ] ; then |
---|
34 | echo "substr: bad argument count" |
---|
35 | return 2 |
---|
36 | fi |
---|
37 | |
---|
38 | str="$1" |
---|
39 | |
---|
40 | # |
---|
41 | # We don't want -f, but we don't want to turn it back on if |
---|
42 | # we didn't have it already |
---|
43 | # |
---|
44 | case "$-" in |
---|
45 | "*f*") |
---|
46 | ;; |
---|
47 | *) |
---|
48 | fng=1 |
---|
49 | set -f |
---|
50 | ;; |
---|
51 | esac |
---|
52 | |
---|
53 | case "$flag" in |
---|
54 | -l) |
---|
55 | str="${str#$pat}" # substr -l pat string |
---|
56 | ;; |
---|
57 | -L) |
---|
58 | str="${str##$pat}" # substr -L pat string |
---|
59 | ;; |
---|
60 | -r) |
---|
61 | str="${str%$pat}" # substr -r pat string |
---|
62 | ;; |
---|
63 | -R) |
---|
64 | str="${str%%$pat}" # substr -R pat string |
---|
65 | ;; |
---|
66 | *) |
---|
67 | str="${str%$2}" # substr string pat |
---|
68 | ;; |
---|
69 | esac |
---|
70 | |
---|
71 | echo "$str" |
---|
72 | |
---|
73 | # |
---|
74 | # If we had file name generation when we started, re-enable it |
---|
75 | # |
---|
76 | if [ "$fng" = "1" ] ; then |
---|
77 | set +f |
---|
78 | fi |
---|
79 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.