-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops_exercises.html
More file actions
361 lines (293 loc) · 26.2 KB
/
loops_exercises.html
File metadata and controls
361 lines (293 loc) · 26.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<html lang="en"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="last-modified" content="2022-01-07 11:08:13 +0000">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- meta "search-domain" used for google site search function google_search() -->
<meta name="search-domain" value="https://swcarpentry.github.iohttps://swcarpentry.github.io/shell-novice">
<style class="anchorjs"></style><link rel="stylesheet" type="text/css" href="https://swcarpentry.github.io/shell-novice/assets/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="https://swcarpentry.github.io/shell-novice/assets/css/bootstrap-theme.css">
<link rel="stylesheet" type="text/css" href="https://swcarpentry.github.io/shell-novice/assets/css/lesson.css">
<link rel="stylesheet" type="text/css" href="https://swcarpentry.github.io/shell-novice/assets/css/syntax.css">
<link rel="license" href="#license-info">
<!-- Favicons for everyone -->
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-57x57.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-114x114.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-144x144.png">
<link rel="apple-touch-icon-precomposed" sizes="60x60" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-60x60.png">
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-120x120.png">
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-76x76.png">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-152x152.png">
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-196x196.png" sizes="196x196">
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-16x16.png" sizes="16x16">
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-128.png" sizes="128x128">
<meta name="application-name" content="Software Carpentry - The Unix Shell">
<meta name="msapplication-TileColor" content="#FFFFFF">
<meta name="msapplication-TileImage" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-144x144.png">
<meta name="msapplication-square70x70logo" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-70x70.png">
<meta name="msapplication-square150x150logo" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-150x150.png">
<meta name="msapplication-wide310x150logo" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-310x150.png">
<meta name="msapplication-square310x310logo" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-310x310.png">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<title>
Loops – The Unix Shell
</title>
</head>
<body>
<div class="container">
<h1 class="maintitle">Loops - Exercises</h1>
<blockquote class="challenge">
<blockquote class="challenge">
<h2 id="write-your-own-loop">Write your own loop<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#write-your-own-loop" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p>How would you write a loop that echoes all 10 numbers from 0 to 9?</p>
<blockquote class="solution">
<h2 id="solution">Solution<span class="fold-unfold glyphicon glyphicon-collapse-down"></span><a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#solution" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<div class="language-bash highlighter-rouge" style="display: none;"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="k">for </span>loop_variable <span class="k">in </span>0 1 2 3 4 5 6 7 8 9
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">echo</span> <span class="nv">$loop_variable</span>
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<div class="language-plaintext output highlighter-rouge" style="display: none;"><div class="highlight"><pre class="highlight"><code>0
1
2
3
4
5
6
7
8
9
</code></pre></div> </div>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="variables-in-loops">Variables in Loops<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#variables-in-loops" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p>This exercise refers to the <code class="language-plaintext highlighter-rouge">shell-lesson-data/molecules</code> directory.
<code class="language-plaintext highlighter-rouge">ls</code> gives the following output:</p>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
</code></pre></div> </div>
<p>What is the output of the following code?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="k">for </span>datafile <span class="k">in</span> <span class="k">*</span>.pdb
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">ls</span> <span class="k">*</span>.pdb
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<p>Now, what is the output of the following code?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="k">for </span>datafile <span class="k">in</span> <span class="k">*</span>.pdb
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">ls</span> <span class="nv">$datafile</span>
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<p>Why do these two loops give different outputs?</p>
<blockquote class="solution">
<h2 id="solution-1">Solution<span class="fold-unfold glyphicon glyphicon-collapse-down"></span><a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#solution-1" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p style="display: none;">The first code block gives the same output on each iteration through
the loop.
Bash expands the wildcard <code class="language-plaintext highlighter-rouge">*.pdb</code> within the loop body (as well as
before the loop starts) to match all files ending in <code class="language-plaintext highlighter-rouge">.pdb</code>
and then lists them using <code class="language-plaintext highlighter-rouge">ls</code>.
The expanded loop would look like this:</p>
<div class="language-bash highlighter-rouge" style="display: none;"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="k">for </span>datafile <span class="k">in </span>cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">ls </span>cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<div class="language-plaintext output highlighter-rouge" style="display: none;"><div class="highlight"><pre class="highlight"><code>cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
cubane.pdb ethane.pdb methane.pdb octane.pdb pentane.pdb propane.pdb
</code></pre></div> </div>
<p style="display: none;">The second code block lists a different file on each loop iteration.
The value of the <code class="language-plaintext highlighter-rouge">datafile</code> variable is evaluated using <code class="language-plaintext highlighter-rouge">$datafile</code>,
and then listed using <code class="language-plaintext highlighter-rouge">ls</code>.</p>
<div class="language-plaintext output highlighter-rouge" style="display: none;"><div class="highlight"><pre class="highlight"><code>cubane.pdb
ethane.pdb
methane.pdb
octane.pdb
pentane.pdb
propane.pdb
</code></pre></div> </div>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="limiting-sets-of-files">Limiting Sets of Files<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#limiting-sets-of-files" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p>What would be the output of running the following loop in the
<code class="language-plaintext highlighter-rouge">shell-lesson-data/molecules</code> directory?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="k">for </span>filename <span class="k">in </span>c<span class="k">*</span>
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">ls</span> <span class="nv">$filename</span>
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<ol>
<li>No files are listed.</li>
<li>All files are listed.</li>
<li>Only <code class="language-plaintext highlighter-rouge">cubane.pdb</code>, <code class="language-plaintext highlighter-rouge">octane.pdb</code> and <code class="language-plaintext highlighter-rouge">pentane.pdb</code> are listed.</li>
<li>Only <code class="language-plaintext highlighter-rouge">cubane.pdb</code> is listed.</li>
</ol>
<blockquote class="solution">
<h2 id="solution-2">Solution<span class="fold-unfold glyphicon glyphicon-collapse-down"></span><a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#solution-2" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p style="display: none;">4 is the correct answer. <code class="language-plaintext highlighter-rouge">*</code> matches zero or more characters, so any file name starting with
the letter c, followed by zero or more other characters will be matched.</p>
</blockquote>
<p>How would the output differ from using this command instead?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="k">for </span>filename <span class="k">in</span> <span class="k">*</span>c<span class="k">*</span>
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">ls</span> <span class="nv">$filename</span>
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<ol>
<li>The same files would be listed.</li>
<li>All the files are listed this time.</li>
<li>No files are listed this time.</li>
<li>The files <code class="language-plaintext highlighter-rouge">cubane.pdb</code> and <code class="language-plaintext highlighter-rouge">octane.pdb</code> will be listed.</li>
<li>Only the file <code class="language-plaintext highlighter-rouge">octane.pdb</code> will be listed.</li>
</ol>
<blockquote class="solution">
<h2 id="solution-3">Solution<span class="fold-unfold glyphicon glyphicon-collapse-up"></span><a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#solution-3" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p style="">4 is the correct answer. <code class="language-plaintext highlighter-rouge">*</code> matches zero or more characters, so a file name with zero or more
characters before a letter c and zero or more characters after the letter c will be matched.</p>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="saving-to-a-file-in-a-loop---part-one">Saving to a File in a Loop - Part One<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#saving-to-a-file-in-a-loop---part-one" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p>In the <code class="language-plaintext highlighter-rouge">shell-lesson-data/molecules</code> directory, what is the effect of this loop?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">for </span>alkanes <span class="k">in</span> <span class="k">*</span>.pdb
<span class="k">do
</span><span class="nb">echo</span> <span class="nv">$alkanes</span>
<span class="nb">cat</span> <span class="nv">$alkanes</span> <span class="o">></span> alkanes.pdb
<span class="k">done</span>
</code></pre></div> </div>
<ol>
<li>Prints <code class="language-plaintext highlighter-rouge">cubane.pdb</code>, <code class="language-plaintext highlighter-rouge">ethane.pdb</code>, <code class="language-plaintext highlighter-rouge">methane.pdb</code>, <code class="language-plaintext highlighter-rouge">octane.pdb</code>, <code class="language-plaintext highlighter-rouge">pentane.pdb</code> and
<code class="language-plaintext highlighter-rouge">propane.pdb</code>, and the text from <code class="language-plaintext highlighter-rouge">propane.pdb</code> will be saved to a file called <code class="language-plaintext highlighter-rouge">alkanes.pdb</code>.</li>
<li>Prints <code class="language-plaintext highlighter-rouge">cubane.pdb</code>, <code class="language-plaintext highlighter-rouge">ethane.pdb</code>, and <code class="language-plaintext highlighter-rouge">methane.pdb</code>, and the text from all three files
would be concatenated and saved to a file called <code class="language-plaintext highlighter-rouge">alkanes.pdb</code>.</li>
<li>Prints <code class="language-plaintext highlighter-rouge">cubane.pdb</code>, <code class="language-plaintext highlighter-rouge">ethane.pdb</code>, <code class="language-plaintext highlighter-rouge">methane.pdb</code>, <code class="language-plaintext highlighter-rouge">octane.pdb</code>, and <code class="language-plaintext highlighter-rouge">pentane.pdb</code>,
and the text from <code class="language-plaintext highlighter-rouge">propane.pdb</code> will be saved to a file called <code class="language-plaintext highlighter-rouge">alkanes.pdb</code>.</li>
<li>None of the above.</li>
</ol>
<blockquote class="solution">
<h2 id="solution-4">Solution<span class="fold-unfold glyphicon glyphicon-collapse-down"></span><a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#solution-4" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<ol style="display: none;">
<li>The text from each file in turn gets written to the <code class="language-plaintext highlighter-rouge">alkanes.pdb</code> file.
However, the file gets overwritten on each loop iteration, so the final content of <code class="language-plaintext highlighter-rouge">alkanes.pdb</code>
is the text from the <code class="language-plaintext highlighter-rouge">propane.pdb</code> file.</li>
</ol>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="saving-to-a-file-in-a-loop---part-two">Saving to a File in a Loop - Part Two<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#saving-to-a-file-in-a-loop---part-two" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p>Also in the <code class="language-plaintext highlighter-rouge">shell-lesson-data/molecules</code> directory,
what would be the output of the following loop?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">for </span>datafile <span class="k">in</span> <span class="k">*</span>.pdb
<span class="k">do
</span><span class="nb">cat</span> <span class="nv">$datafile</span> <span class="o">>></span> all.pdb
<span class="k">done</span>
</code></pre></div> </div>
<ol>
<li>All of the text from <code class="language-plaintext highlighter-rouge">cubane.pdb</code>, <code class="language-plaintext highlighter-rouge">ethane.pdb</code>, <code class="language-plaintext highlighter-rouge">methane.pdb</code>, <code class="language-plaintext highlighter-rouge">octane.pdb</code>, and
<code class="language-plaintext highlighter-rouge">pentane.pdb</code> would be concatenated and saved to a file called <code class="language-plaintext highlighter-rouge">all.pdb</code>.</li>
<li>The text from <code class="language-plaintext highlighter-rouge">ethane.pdb</code> will be saved to a file called <code class="language-plaintext highlighter-rouge">all.pdb</code>.</li>
<li>All of the text from <code class="language-plaintext highlighter-rouge">cubane.pdb</code>, <code class="language-plaintext highlighter-rouge">ethane.pdb</code>, <code class="language-plaintext highlighter-rouge">methane.pdb</code>, <code class="language-plaintext highlighter-rouge">octane.pdb</code>, <code class="language-plaintext highlighter-rouge">pentane.pdb</code>
and <code class="language-plaintext highlighter-rouge">propane.pdb</code> would be concatenated and saved to a file called <code class="language-plaintext highlighter-rouge">all.pdb</code>.</li>
<li>All of the text from <code class="language-plaintext highlighter-rouge">cubane.pdb</code>, <code class="language-plaintext highlighter-rouge">ethane.pdb</code>, <code class="language-plaintext highlighter-rouge">methane.pdb</code>, <code class="language-plaintext highlighter-rouge">octane.pdb</code>, <code class="language-plaintext highlighter-rouge">pentane.pdb</code>
and <code class="language-plaintext highlighter-rouge">propane.pdb</code> would be printed to the screen and saved to a file called <code class="language-plaintext highlighter-rouge">all.pdb</code>.</li>
</ol>
<blockquote class="solution">
<h2 id="solution-5">Solution<span class="fold-unfold glyphicon glyphicon-collapse-down"></span><a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#solution-5" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p style="display: none;">3 is the correct answer. <code class="language-plaintext highlighter-rouge">>></code> appends to a file, rather than overwriting it with the redirected
output from a command.
Given the output from the <code class="language-plaintext highlighter-rouge">cat</code> command has been redirected, nothing is printed to the screen.</p>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="doing-a-dry-run">Doing a Dry Run<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#doing-a-dry-run" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p>A loop is a way to do many things at once — or to make many mistakes at
once if it does the wrong thing. One way to check what a loop <em>would</em> do
is to <code class="language-plaintext highlighter-rouge">echo</code> the commands it would run instead of actually running them.</p>
<p>Suppose we want to preview the commands the following loop will execute
without actually running those commands:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="k">for </span>datafile <span class="k">in</span> <span class="k">*</span>.pdb
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">cat</span> <span class="nv">$datafile</span> <span class="o">>></span> all.pdb
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<p>What is the difference between the two loops below, and which one would we
want to run?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># Version 1</span>
<span class="nv">$ </span><span class="k">for </span>datafile <span class="k">in</span> <span class="k">*</span>.pdb
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">echo cat</span> <span class="nv">$datafile</span> <span class="o">>></span> all.pdb
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c"># Version 2</span>
<span class="nv">$ </span><span class="k">for </span>datafile <span class="k">in</span> <span class="k">*</span>.pdb
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">echo</span> <span class="s2">"cat </span><span class="nv">$datafile</span><span class="s2"> >> all.pdb"</span>
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<blockquote class="solution">
<h2 id="solution-6">Solution<span class="fold-unfold glyphicon glyphicon-collapse-down"></span><a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#solution-6" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p style="display: none;">The second version is the one we want to run.
This prints to screen everything enclosed in the quote marks, expanding the
loop variable name because we have prefixed it with a dollar sign.
It also <em>does not</em> modify nor create the file <code class="language-plaintext highlighter-rouge">all.pdb</code>, as the <code class="language-plaintext highlighter-rouge">>></code>
is treated literally as part of a string rather than as a
redirection instruction.</p>
<p style="display: none;">The first version appends the output from the command <code class="language-plaintext highlighter-rouge">echo cat $datafile</code>
to the file, <code class="language-plaintext highlighter-rouge">all.pdb</code>. This file will just contain the list;
<code class="language-plaintext highlighter-rouge">cat cubane.pdb</code>, <code class="language-plaintext highlighter-rouge">cat ethane.pdb</code>, <code class="language-plaintext highlighter-rouge">cat methane.pdb</code> etc.</p>
<p style="display: none;">Try both versions for yourself to see the output! Be sure to open the
<code class="language-plaintext highlighter-rouge">all.pdb</code> file to view its contents.</p>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="nested-loops">Nested Loops<a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#nested-loops" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p>Suppose we want to set up a directory structure to organize
some experiments measuring reaction rate constants with different compounds
<em>and</em> different temperatures. What would be the
result of the following code:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="k">for </span>species <span class="k">in </span>cubane ethane methane
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="k">for </span>temperature <span class="k">in </span>25 30 37 40
<span class="o">></span> <span class="k">do</span>
<span class="o">></span> <span class="nb">mkdir</span> <span class="nv">$species</span>-<span class="nv">$temperature</span>
<span class="o">></span> <span class="k">done</span>
<span class="o">></span> <span class="k">done</span>
</code></pre></div> </div>
<blockquote class="solution">
<h2 id="solution-7">Solution<span class="fold-unfold glyphicon glyphicon-collapse-down"></span><a class="anchorjs-link " aria-label="Anchor" data-anchorjs-icon="" href="#solution-7" style="font: 1em / 1 anchorjs-icons; padding-left: 0.375em;"></a></h2>
<p style="display: none;">We have a nested loop, i.e. contained within another loop, so for each species
in the outer loop, the inner loop (the nested loop) iterates over the list of
temperatures, and creates a new directory for each combination.</p>
<p style="display: none;">Try running the code for yourself to see which directories are created!</p>
</blockquote>
</blockquote>
</blockquote>
</div>
<script async="" src="https://www.google-analytics.com/analytics.js"></script><script src="https://swcarpentry.github.io/shell-novice/assets/js/jquery.min.js"></script>
<script src="https://swcarpentry.github.io/shell-novice/assets/js/bootstrap.min.js"></script>
<script src="https://swcarpentry.github.io/shell-novice/assets/js/lesson.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-37305346-2', 'auto');
ga('send', 'pageview');
</script>
<script src="https://swcarpentry.github.io/shell-novice/assets/js/anchor.min.js"></script>
<script>
anchors.add();
</script>
</body></html>