Skip to content

Commit cc50d52

Browse files
committed
Adding a real-world example and possible drawbacks.
Also replacing the image in the concept readme with a mermaid diagram.
1 parent 1debd14 commit cc50d52

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

concepts/README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,24 @@
22

33
The [plan](http://forum.exercism.org/t/bash-syllabus-planning/11952)
44

5-
The suggested concept flow:
6-
7-
[![bash syllabus concept flowchart](https://glennj.github.io/img/bash.syllabus.flow.png)](http://forum.exercism.org/t/bash-syllabus-flow/15038)
5+
## Concept Flow:
6+
7+
```mermaid
8+
erDiagram
9+
"Commands and Arguments" ||--|| Variables : ""
10+
Variables ||--|| "The Importance of Quoting" : ""
11+
"The Importance of Quoting" ||--|| Conditionals : ""
12+
"The Importance of Quoting" ||--|| Arrays : ""
13+
"The Importance of Quoting" ||--|| "Pipelines and Command Lists" : ""
14+
Conditionals ||--|| Arithmetic : ""
15+
Conditionals ||--|| Looping : ""
16+
Arrays ||--|| "More About Arrays" : ""
17+
"Pipelines and Command Lists" ||--|| Functions : ""
18+
Functions ||--|| Redirection : ""
19+
Redirection ||--|| "Command Substitution" : TODO
20+
Redirection ||--|| "Here Documents" : ""
21+
"Command Substitution" ||--|| "Process Substitution" : TODO
22+
```
823

924
1. Basic syntax: commands and arguments
1025

concepts/heredocs/about.md

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,34 @@ Two plus two is calculated by $((2 + 2))
152152

153153
### Stripping Leading Tabs
154154

155-
If you use `<<-` (with a trailing hyphen) instead of `<<`, Bash will strip leading _tab characters_ from each line of the heredoc.
155+
If you use `<<-` (with a trailing hyphen) instead of `<<`, Bash will strip any leading _tab characters_ from each line of the heredoc.
156156
This is useful for indenting the heredoc content within your script without affecting the output.
157157

158158
```bash
159159
# Note, the leading whitespace is tab characters only, not spaces!
160-
# and the ending delimiter can have leading tabs as well
160+
# The ending delimiter can have leading tabs as well.
161161
cat <<- END
162-
This line has a leading tab.
163-
This line also has a leading tab.
164-
END
162+
This line has 1 leading tab.
163+
This line has a leading tab and some spaces.
164+
This line 2 leading tabs.
165+
END
165166
```
166167

167-
The output is printed without the leading tabs:
168+
The output is printed with all the leading tabs removed:
168169

169170
```plaintext
170-
This line has a leading tab.
171-
This line also has a leading tab.
171+
This line has 1 leading tab.
172+
This line has a leading tab and some spaces.
173+
This line 2 leading tabs.
172174
```
173175

176+
~~~~exercism/caution
174177
The author doesn't recommend this usage.
175-
While it can improve the readability of the script, it's too easy to accidentally replace the tab characters with spaces and it's too hard to spot the difference.
178+
While it can improve the readability of the script,
179+
180+
1. it's too easy to accidentally replace the tab characters with spaces (your editor may do this automatically), and
181+
1. it's too hard to spot the difference between spaces and tabs.
182+
~~~~
176183

177184
## When to Use Here Documents
178185

@@ -182,6 +189,37 @@ While it can improve the readability of the script, it's too easy to accidentall
182189
* Scripting interactions: simulating user input for interactive programs.
183190
* Avoiding external files: when you want to avoid creating temporary files.
184191

192+
A typical usage might be to provide some help text:
193+
194+
```bash
195+
#!/usr/bin/env bash
196+
197+
usage() {
198+
cat << END_USAGE
199+
Refresh database tables.
200+
201+
usage: $(basename "$0") [-h|--help] [-A|--no-archive]
202+
203+
where: --no-archive flag will _skip_ the archive jobs
204+
END_USAGE
205+
}
206+
207+
# ... parsing command line options here ...
208+
209+
if [[ $flag_help == true" ]]; then
210+
usage
211+
exit 0
212+
fi
213+
```
214+
215+
## Possible Drawbacks
216+
217+
* Large embedded documents can make your code harder to read.
218+
It can be better to deploy your script with separate documentation files.
219+
* Here documents can break the flow of the code.
220+
You might be in a deeply nested section of code, and you want to pass some text to a program.
221+
The heredoc's indentation can look jarring compared to the surrounding code.
222+
185223
## Here Strings
186224
187225
Like heredocs, _here strings_ provide input to a command.
@@ -255,9 +293,9 @@ done >> data.csv
255293
Note the position of the output redirection.
256294
All output from the while loop will be appended to the file `data.csv`.
257295
258-
## Heredocs and Herestrings are Redirections
296+
## Heredocs and Herestrings as Redirection
259297
260-
Because they are just redirections, they can be combined with other redirections:
298+
Because these are just forms of redirection, they can be combined with other redirection operations:
261299
262300
```bash
263301
cat <<< END_OF_TEXT > output.txt

0 commit comments

Comments
 (0)