1 |
#!/usr/bin/env bash |
2 |
# |
3 |
# Test combination of var ops. |
4 |
|
5 |
### String length |
6 |
v=foo |
7 |
echo ${#v} |
8 |
# stdout: 3 |
9 |
|
10 |
### Length of undefined variable |
11 |
echo ${#undef} |
12 |
# stdout: 0 |
13 |
|
14 |
### Length of undefined variable with nounset |
15 |
set -o nounset |
16 |
echo ${#undef} |
17 |
# status: 1 |
18 |
# OK dash status: 2 |
19 |
|
20 |
### Cannot take length of substring slice |
21 |
# These are runtime errors, but we could make them parse time errors. |
22 |
v=abcde |
23 |
echo ${#v:1:3} |
24 |
# status: 1 |
25 |
# N-I dash status: 0 |
26 |
# N-I dash stdout: 5 |
27 |
|
28 |
### Pattern replacement |
29 |
v=abcde |
30 |
echo ${v/c*/XX} |
31 |
# stdout: abXX |
32 |
# N-I dash status: 2 |
33 |
# N-I dash stdout-json: "" |
34 |
|
35 |
### Global Pattern replacement with / |
36 |
s=xx_xx_xx |
37 |
echo ${s/xx?/yy_} ${s//xx?/yy_} |
38 |
# stdout: yy_xx_xx yy_yy_xx |
39 |
# N-I dash status: 2 |
40 |
# N-I dash stdout-json: "" |
41 |
|
42 |
### Left Anchored Pattern replacement with # |
43 |
s=xx_xx_xx |
44 |
echo ${s/?xx/_yy} ${s/#?xx/_yy} |
45 |
# stdout: xx_yy_xx xx_xx_xx |
46 |
# N-I dash status: 2 |
47 |
# N-I dash stdout-json: "" |
48 |
|
49 |
### Right Anchored Pattern replacement with % |
50 |
s=xx_xx_xx |
51 |
echo ${s/?xx/_yy} ${s/%?xx/_yy} |
52 |
# stdout: xx_yy_xx xx_xx_yy |
53 |
# N-I dash status: 2 |
54 |
# N-I dash stdout-json: "" |
55 |
|
56 |
### Replace fixed strings |
57 |
s=xx_xx |
58 |
echo ${s/xx/yy} ${s//xx/yy} ${s/#xx/yy} ${s/%xx/yy} |
59 |
# stdout: yy_xx yy_yy yy_xx xx_yy |
60 |
# N-I dash status: 2 |
61 |
# N-I dash stdout-json: "" |
62 |
|
63 |
### Replace is longest match |
64 |
# If it were shortest, then you would just replace the first <html> |
65 |
s='begin <html></html> end' |
66 |
echo ${s/<*>/[]} |
67 |
# stdout: begin [] end |
68 |
# N-I dash status: 2 |
69 |
# N-I dash stdout-json: "" |
70 |
|
71 |
### Replace char class |
72 |
s=xx_xx_xx |
73 |
echo ${s//[[:alpha:]]/y} ${s//[^[:alpha:]]/-} |
74 |
# stdout: yy_yy_yy xx-xx-xx |
75 |
# N-I mksh stdout: xx_xx_xx xx_xx_xx |
76 |
# N-I dash status: 2 |
77 |
# N-I dash stdout-json: "" |
78 |
|
79 |
### Pattern replacement ${v/} is not valid |
80 |
v=abcde |
81 |
echo -${v/}- |
82 |
echo status=$? |
83 |
# status: 2 |
84 |
# stdout-json: "" |
85 |
# N-I dash status: 2 |
86 |
# N-I dash stdout-json: "" |
87 |
# BUG bash/mksh status: 0 |
88 |
# BUG bash/mksh stdout-json: "-abcde-\nstatus=0\n" |
89 |
|
90 |
### Pattern replacement ${v//} is not valid |
91 |
v='a/b/c' |
92 |
echo -${v//}- |
93 |
echo status=$? |
94 |
# status: 2 |
95 |
# stdout-json: "" |
96 |
# N-I dash status: 2 |
97 |
# N-I dash stdout-json: "" |
98 |
# BUG bash/mksh status: 0 |
99 |
# BUG bash/mksh stdout-json: "-a/b/c-\nstatus=0\n" |
100 |
|
101 |
### ${v/a} is the same as ${v/a/} -- no replacement string |
102 |
v='aabb' |
103 |
echo ${v/a} |
104 |
echo status=$? |
105 |
# stdout-json: "abb\nstatus=0\n" |
106 |
# N-I dash stdout-json: "" |
107 |
# N-I dash status: 2 |
108 |
|
109 |
### String slice |
110 |
foo=abcdefg |
111 |
echo ${foo:1:3} |
112 |
# stdout: bcd |
113 |
# N-I dash status: 2 |
114 |
# N-I dash stdout-json: "" |
115 |
|
116 |
### Negative string slice |
117 |
foo=abcdefg |
118 |
echo ${foo: -4:3} |
119 |
# stdout: def |
120 |
# N-I dash status: 2 |
121 |
# N-I dash stdout-json: "" |
122 |
|
123 |
### String slice with math |
124 |
# I think this is the $(()) language inside? |
125 |
i=1 |
126 |
foo=abcdefg |
127 |
echo ${foo: i-3-2 : i + 2} |
128 |
# stdout: def |
129 |
# N-I dash status: 2 |
130 |
# N-I dash stdout-json: "" |