OILS / test / gold / complex-here-docs.sh View on Github | oilshell.org

192 lines, 160 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# ./09-here-doc.sh <function name>
5
6foo=bar
7
8cat <<< 'here string'
9echo
10
11# var interpolation
12cat <<EOF
13plain EOF terminator: $foo
14EOF
15
16echo
17
18# no var interpolation for any quoted delimiter
19cat <<'EOF'
20single quoted: $foo
21EOF
22cat <<"EOF"
23double-quoted: $foo
24EOF
25cat <<\EOF
26E escaped: $foo
27EOF
28cat <<EO\F
29F escaped: $foo
30EOF
31
32echo
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
52echo
53
54# output is bar and then foo
55cat <<EOF; echo 'command on same physical line as here doc'
56HERE CONTENTS
57EOF
58
59echo
60
61# Line continuation is ALLOWED in command before here doc.
62cat <<EOF\
63 ; echo 'command on same logical line as here delimiter, after line continuation'
64HERE CONTENTS
65EOF
66
67echo
68
69cat <<EOF | tac; echo PIPE ON SAME LINE
70PIPE 1
71PIPE 2
72EOF
73echo
74
75cat <<EOF |
76PIPE 1
77PIPE 2
78EOF
79tac; echo PIPE ON DIFFERENT LINE
80echo
81
82tac <<EOF1 && tac <<EOF2
83PIPE A1
84PIPE A2
85EOF1
86PIPE B1
87PIPE B2
88EOF2
89echo
90
91cat <<EOF && echo '&&'
92Here doc in first part of &&
93EOF
94echo
95
96if cat <<EOF; then
97here doc in IF CONDITION
98EOF
99 echo THEN executed
100fi
101echo
102
103{ cat <<EOF; }; echo BLOCK
104here doc in BLOCK
105EOF
106echo
107
108( cat <<EOF ); echo SUBSHELL
109here doc in SUBSHELL
110EOF
111echo
112
113myfunc() {
114 cat <<EOF; echo in function
115here doc in FUNCTION
116EOF
117}
118myfunc
119echo
120
121case x in
122 x) cat <<EOF; echo CASE
123here doc in CASE
124EOF
125 ;;
126esac
127echo
128
129while read line; do
130 echo == ${line} ==
131done <<EOF
132while 1
133while 2
134EOF
135echo
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
143for x in 1 2 $(cat <<EOF
144THREE
145EOF
146); do
147 echo for word $x
148done
149echo
150
151if cat <<EOF1; then echo THEN; cat <<EOF2; fi
152here doc 1
153EOF1
154here doc 2
155EOF2
156echo
157
158# NESTING
159if cat <<EOF1 && cat <<EOF2; then echo THEN; cat <<EOF3; fi
160here doc 1
161EOF1
162here doc 2
163EOF2
164here doc 3
165EOF3
166echo
167
168# Here doc within here doc
169cat <<EOF
170one
171two
172echo $(cat <<EOF2
173INNER
174EOF2
175)
176three
177four
178EOF
179echo
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.
183while cat <<EOF1; read line; do cat <<EOF2; echo "read line: '$line'"; done <<EOF3
184condition here doc
185EOF1
186body here doc
187EOF2
188while loop here doc 1
189while loop here doc 2
190EOF3
191
192echo == DONE HERE DOC TESTS ==