Skip to content

Commit 3d4b312

Browse files
fredrikekreKristofferC
authored andcommitted
doctests for running-external-programs.md (#20315)
1 parent 974c3d7 commit 3d4b312

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

doc/src/manual/running-external-programs.md

+35-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Julia borrows backtick notation for commands from the shell, Perl, and Ruby. However, in Julia,
44
writing
55

6-
```julia
6+
```jldoctest
77
julia> `echo hello`
88
`echo hello`
99
```
@@ -22,8 +22,14 @@ differs in several aspects from the behavior in various shells, Perl, or Ruby:
2222

2323
Here's a simple example of running an external program:
2424

25-
```julia
26-
julia> run(`echo hello`)
25+
```jldoctest
26+
julia> mycommand = `echo hello`
27+
`echo hello`
28+
29+
julia> typeof(mycommand)
30+
Cmd
31+
32+
julia> run(mycommand)
2733
hello
2834
```
2935

@@ -33,11 +39,11 @@ successfully.
3339

3440
If you want to read the output of the external command, [`readstring()`](@ref) can be used instead:
3541

36-
```julia
37-
julia> a=readstring(`echo hello`)
42+
```jldoctest
43+
julia> a = readstring(`echo hello`)
3844
"hello\n"
3945
40-
julia> (chomp(a)) == "hello"
46+
julia> chomp(a) == "hello"
4147
true
4248
```
4349

@@ -60,7 +66,7 @@ Suppose you want to do something a bit more complicated and use the name of a fi
6066
`file` as an argument to a command. You can use `$` for interpolation much as you would in a string
6167
literal (see [Strings](@ref)):
6268

63-
```julia
69+
```jldoctest
6470
julia> file = "/etc/passwd"
6571
"/etc/passwd"
6672
@@ -73,7 +79,7 @@ that are special to the shell, they may cause undesirable behavior. Suppose, for
7379
than `/etc/passwd`, we wanted to sort the contents of the file `/Volumes/External HD/data.csv`.
7480
Let's try it:
7581

76-
```julia
82+
```jldoctest
7783
julia> file = "/Volumes/External HD/data.csv"
7884
"/Volumes/External HD/data.csv"
7985
@@ -87,7 +93,7 @@ is never interpreted by a shell, so there's no need for actual quoting; the quot
8793
only for presentation to the user. This will even work if you interpolate a value as part of a
8894
shell word:
8995

90-
```julia
96+
```jldoctest
9197
julia> path = "/Volumes/External HD"
9298
"/Volumes/External HD"
9399
@@ -104,7 +110,7 @@ julia> `sort $path/$name.$ext`
104110
As you can see, the space in the `path` variable is appropriately escaped. But what if you *want*
105111
to interpolate multiple words? In that case, just use an array (or any other iterable container):
106112

107-
```julia
113+
```jldoctest
108114
julia> files = ["/etc/passwd","/Volumes/External HD/data.csv"]
109115
2-element Array{String,1}:
110116
"/etc/passwd"
@@ -117,7 +123,7 @@ julia> `grep foo $files`
117123
If you interpolate an array as part of a shell word, Julia emulates the shell's `{a,b,c}` argument
118124
generation:
119125

120-
```julia
126+
```jldoctest
121127
julia> names = ["foo","bar","baz"]
122128
3-element Array{String,1}:
123129
"foo"
@@ -131,7 +137,7 @@ julia> `grep xylophone $names.txt`
131137
Moreover, if you interpolate multiple arrays into the same word, the shell's Cartesian product
132138
generation behavior is emulated:
133139

134-
```julia
140+
```jldoctest
135141
julia> names = ["foo","bar","baz"]
136142
3-element Array{String,1}:
137143
"foo"
@@ -150,7 +156,7 @@ julia> `rm -f $names.$exts`
150156
Since you can interpolate literal arrays, you can use this generative functionality without needing
151157
to create temporary array objects first:
152158

153-
```julia
159+
```jldoctest
154160
julia> `rm -rf $["foo","bar","baz","qux"].$["aux","log","pdf"]`
155161
`rm -rf foo.aux foo.log foo.pdf bar.aux bar.log bar.pdf baz.aux baz.log baz.pdf qux.aux qux.log qux.pdf`
156162
```
@@ -187,22 +193,22 @@ behaviors are the same as the shell's. The only difference is that the interpola
187193
and aware of Julia's notion of what is a single string value, and what is a container for multiple
188194
values. Let's try the above two examples in Julia:
189195

190-
```julia
191-
julia> `perl -le '$|=1; for (0..3) { print }'`
196+
```jldoctest
197+
julia> A = `perl -le '$|=1; for (0..3) { print }'`
192198
`perl -le '$|=1; for (0..3) { print }'`
193199
194-
julia> run(ans)
200+
julia> run(A)
195201
0
196202
1
197203
2
198204
3
199205
200206
julia> first = "A"; second = "B";
201207
202-
julia> `perl -le 'print for @ARGV' "1: $first" "2: $second"`
208+
julia> B = `perl -le 'print for @ARGV' "1: $first" "2: $second"`
203209
`perl -le 'print for @ARGV' '1: A' '2: B'`
204210
205-
julia> run(ans)
211+
julia> run(B)
206212
1: A
207213
2: B
208214
```
@@ -215,20 +221,21 @@ easily and safely just examine its interpretation without doing any damage.
215221

216222
## Pipelines
217223

218-
Shell metacharacters, such as `|`, `&`, and `>`, are not special inside of Julia's backticks:
219-
unlike in the shell, inside of Julia's backticks, a pipe is always just a pipe:
224+
Shell metacharacters, such as `|`, `&`, and `>`, need to be quoted (or escaped) inside of Julia's backticks:
220225

221-
```julia
222-
julia> run(`echo hello | sort`)
226+
```jldoctest
227+
julia> run(`echo hello '|' sort`)
228+
hello | sort
229+
230+
julia> run(`echo hello \| sort`)
223231
hello | sort
224232
```
225233

226-
This expression invokes the `echo` command with three words as arguments: "hello", "|", and "sort".
227-
The result is that a single line is printed: "hello | sort". Inside of backticks, a "|" is just
228-
a literal pipe character. How, then, does one construct a pipeline? Instead of using "|" inside
229-
of backticks, one uses [`pipeline()`](@ref):
234+
This expression invokes the `echo` command with three words as arguments: `hello`, `|`, and `sort`.
235+
The result is that a single line is printed: `hello | sort`. How, then, does one construct a
236+
pipeline? Instead of using `'|'` inside of backticks, one uses [`pipeline()`](@ref):
230237

231-
```julia
238+
```jldoctest
232239
julia> run(pipeline(`echo hello`, `sort`))
233240
hello
234241
```
@@ -265,7 +272,7 @@ nearly simultaneously, and race to make the first write to the [`STDOUT`](@ref)
265272
share with each other and the `julia` parent process. Julia lets you pipe the output from both
266273
of these processes to another program:
267274

268-
```julia
275+
```jldoctest
269276
julia> run(pipeline(`echo world` & `echo hello`, `sort`))
270277
hello
271278
world

0 commit comments

Comments
 (0)