1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Usage:
|
4 | # ./09-here-doc.sh <function name>
|
5 |
|
6 | foo=bar
|
7 |
|
8 | cat <<< 'here string'
|
9 | echo
|
10 |
|
11 | # var interpolation
|
12 | cat <<EOF
|
13 | plain EOF terminator: $foo
|
14 | EOF
|
15 |
|
16 | echo
|
17 |
|
18 | # no var interpolation for any quoted delimiter
|
19 | cat <<'EOF'
|
20 | single quoted: $foo
|
21 | EOF
|
22 | cat <<"EOF"
|
23 | double-quoted: $foo
|
24 | EOF
|
25 | cat <<\EOF
|
26 | E escaped: $foo
|
27 | EOF
|
28 | cat <<EO\F
|
29 | F escaped: $foo
|
30 | EOF
|
31 |
|
32 | echo
|
33 |
|
34 | # Strip leading tabs
|
35 | cat <<-EOF
|
36 | one tab then foo: $foo
|
37 | EOF
|
38 |
|
39 | cat <<-EOF
|
40 | two tabs then foo: $foo
|
41 | EOF
|
42 |
|
43 | cat <<-EOF
|
44 | two spaces then a tab: $foo
|
45 | EOF
|
46 |
|
47 | # Strip leading and no interpolation
|
48 | cat <<-\EOF
|
49 | one tab and no interpolation: $foo
|
50 | EOF
|
51 |
|
52 | echo
|
53 |
|
54 | # output is bar and then foo
|
55 | cat <<EOF; echo 'command on same physical line as here doc'
|
56 | HERE CONTENTS
|
57 | EOF
|
58 |
|
59 | echo
|
60 |
|
61 | # Line continuation is ALLOWED in command before here doc.
|
62 | cat <<EOF\
|
63 | ; echo 'command on same logical line as here delimiter, after line continuation'
|
64 | HERE CONTENTS
|
65 | EOF
|
66 |
|
67 | echo
|
68 |
|
69 | cat <<EOF | tac; echo PIPE ON SAME LINE
|
70 | PIPE 1
|
71 | PIPE 2
|
72 | EOF
|
73 | echo
|
74 |
|
75 | cat <<EOF |
|
76 | PIPE 1
|
77 | PIPE 2
|
78 | EOF
|
79 | tac; echo PIPE ON DIFFERENT LINE
|
80 | echo
|
81 |
|
82 | tac <<EOF1 && tac <<EOF2
|
83 | PIPE A1
|
84 | PIPE A2
|
85 | EOF1
|
86 | PIPE B1
|
87 | PIPE B2
|
88 | EOF2
|
89 | echo
|
90 |
|
91 | cat <<EOF && echo '&&'
|
92 | Here doc in first part of &&
|
93 | EOF
|
94 | echo
|
95 |
|
96 | if cat <<EOF; then
|
97 | here doc in IF CONDITION
|
98 | EOF
|
99 | echo THEN executed
|
100 | fi
|
101 | echo
|
102 |
|
103 | { cat <<EOF; }; echo BLOCK
|
104 | here doc in BLOCK
|
105 | EOF
|
106 | echo
|
107 |
|
108 | ( cat <<EOF ); echo SUBSHELL
|
109 | here doc in SUBSHELL
|
110 | EOF
|
111 | echo
|
112 |
|
113 | myfunc() {
|
114 | cat <<EOF; echo in function
|
115 | here doc in FUNCTION
|
116 | EOF
|
117 | }
|
118 | myfunc
|
119 | echo
|
120 |
|
121 | case x in
|
122 | x) cat <<EOF; echo CASE
|
123 | here doc in CASE
|
124 | EOF
|
125 | ;;
|
126 | esac
|
127 | echo
|
128 |
|
129 | while read line; do
|
130 | echo == ${line} ==
|
131 | done <<EOF
|
132 | while 1
|
133 | while 2
|
134 | EOF
|
135 | echo
|
136 |
|
137 | # NOTE: bash gives a spurious warning here, but executes it correctly:
|
138 | # tests/09-here-doc.sh: line 131: warning: here-document at line 129 delimited
|
139 | # by end-of-file (wanted `EOF')
|
140 | #
|
141 | # Should be EOF\n) though.
|
142 |
|
143 | for x in 1 2 $(cat <<EOF
|
144 | THREE
|
145 | EOF
|
146 | ); do
|
147 | echo for word $x
|
148 | done
|
149 | echo
|
150 |
|
151 | if cat <<EOF1; then echo THEN; cat <<EOF2; fi
|
152 | here doc 1
|
153 | EOF1
|
154 | here doc 2
|
155 | EOF2
|
156 | echo
|
157 |
|
158 | # NESTING
|
159 | if cat <<EOF1 && cat <<EOF2; then echo THEN; cat <<EOF3; fi
|
160 | here doc 1
|
161 | EOF1
|
162 | here doc 2
|
163 | EOF2
|
164 | here doc 3
|
165 | EOF3
|
166 | echo
|
167 |
|
168 | # Here doc within here doc
|
169 | cat <<EOF
|
170 | one
|
171 | two
|
172 | echo $(cat <<EOF2
|
173 | INNER
|
174 | EOF2
|
175 | )
|
176 | three
|
177 | four
|
178 | EOF
|
179 | echo
|
180 |
|
181 | # COMPOUND here docs mixed with individual here docs
|
182 | # This shows it has to be a depth first search, but POST ORDER TRAVERSAL.
|
183 | while cat <<EOF1; read line; do cat <<EOF2; echo "read line: '$line'"; done <<EOF3
|
184 | condition here doc
|
185 | EOF1
|
186 | body here doc
|
187 | EOF2
|
188 | while loop here doc 1
|
189 | while loop here doc 2
|
190 | EOF3
|
191 |
|
192 | echo == DONE HERE DOC TESTS ==
|