You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In teaching today, our group of Library Carpentry instructors all agreed that we would prefer to use .txt instead of .doc files because we'd like to encourage use of non-proprietary files.
Copy file name to clipboardExpand all lines: _episodes/04-loops.md
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -17,17 +17,17 @@ keypoints:
17
17
**Loops** are key to productivity improvements through automation as they allow us to execute
18
18
commands repetitively. Similar to wildcards and tab completion, using loops also reduces the
19
19
amount of typing (and typing mistakes).
20
-
Suppose we have several hundred document files named `project_1825.doc`, `project_1863.doc`, `XML_project.doc`and so on.
20
+
Suppose we have several hundred document files named `project_1825.txt`, `project_1863.txt`, `XML_project.txt`and so on.
21
21
We would like to change these files, but also save a version of the original files, naming the copies
22
-
`backup_project_1825.doc` and so on.
22
+
`backup_project_1825.txt` and so on.
23
23
24
24
We can use a **loop** to do that.
25
25
Here's a simple example that creates a backup copy of four text files in turn.
26
26
27
27
Let's first create those files:
28
28
29
29
~~~
30
-
$ touch a.doc b.doc c.doc d.doc
30
+
$ touch a.txt b.txt c.txt d.txt
31
31
~~~
32
32
This will create four empty files with those names. It is easy to use the shell to create a batch of files in one go.
33
33
@@ -45,7 +45,7 @@ We can apply this to our example like this:
45
45
46
46
47
47
~~~
48
-
$ for filename in *.doc
48
+
$ for filename in *.txt
49
49
> do
50
50
> echo "$filename"
51
51
> cp "$filename" backup_"$filename"
@@ -54,10 +54,10 @@ $ for filename in *.doc
54
54
{: .bash}
55
55
56
56
~~~
57
-
a.doc
58
-
b.doc
59
-
c.doc
60
-
d.doc
57
+
a.txt
58
+
b.txt
59
+
c.txt
60
+
d.txt
61
61
~~~
62
62
{: .output}
63
63
@@ -84,14 +84,14 @@ rather than treat it as text or an external command.
84
84
> enter your loop variable (such as in [episode 5]({{ page.root }}/05-counting-mining/index.html#using-a-loop-to-count-words)).
85
85
{: .callout}
86
86
87
-
In this example, the list is four filenames: 'a.doc', 'b.doc', 'c.doc', and 'd.doc'
87
+
In this example, the list is four filenames: 'a.txt', 'b.txt', 'c.txt', and 'd.txt'
88
88
Each time the loop iterates, it will assign a file name to the variable `filename`
89
89
and run the `cp` command.
90
90
The first time through the loop,
91
-
`$filename` is `a.doc`.
92
-
The interpreter prints the filename to the screen and then runs the command `cp` on `a.doc`, (because we asked it to echo each filename as it works its way through the loop).
91
+
`$filename` is `a.txt`.
92
+
The interpreter prints the filename to the screen and then runs the command `cp` on `a.txt`, (because we asked it to echo each filename as it works its way through the loop).
93
93
For the second iteration, `$filename` becomes
94
-
`b.doc`. This time, the shell prints the filename `b.doc` to the screen, then runs `cp` on `b.doc`. The loop performs the same operations for `c.doc` and then for `d.doc` and then, since
94
+
`b.txt`. This time, the shell prints the filename `b.txt` to the screen, then runs `cp` on `b.txt`. The loop performs the same operations for `c.txt` and then for `d.txt` and then, since
95
95
the list only included these four items, the shell exits the `for` loop at that point.
0 commit comments