include(joinpath(@__DIR__, "observations.jl"))
function child(args...)
runtime = Base.julia_cmd()
script = joinpath(@__DIR__,"command-child.jl")
return `$runtime --startup-file=no --history-file=no --threads=1 $script $args`
end
mktempdir() do folder
marker = joinpath(folder,"child created.txt")
writer = child("mark",marker)
probe("cmd_create", "constructed Cmd before run", (writer isa Cmd,isfile(marker)),(true,false))
run(writer)
probe("cmd_run", "write marker in child", read(marker,String),"created")
failure = child("fail")
failed = outcome(() -> run(failure))
ignored = run(Cmd(failure;ignorestatus=true))
probe("cmd_fail", 7, (failed,ignored.exitcode), ("ProcessFailedException",7))
source = child("lines","plum","apple")
raw = read(source,String)
probe("cmd_read", ("plum","apple"),(raw,readchomp(source)),("plum\napple\n","plum\napple"))
paths = ["red apple.txt","pear.txt"]
probe("cmd_arguments",paths,readchomp(child("args",paths...)),"2|red apple.txt|pear.txt")
sorted = pipeline(source,child("sort"))
probe("cmd_pipe", ("plum","apple"),readchomp(sorted),"apple\nplum")
count = parse(Int,readchomp(pipeline(source,child("count"))))
syntax = Meta.parse("`source | count`";raise=false)
probe("cmd_count", "two input lines and unescaped pipe syntax", (count,outcome(() -> macroexpand(Main,syntax)) != "no error"),(2,true))
destination = joinpath(folder,"sorted output.txt")
run(pipeline(sorted,destination))
probe("cmd_redirect", "sorted standard output",read(destination,String),"apple\nplum\n")
combined = pipeline(child("lines","north") & child("lines","east"), child("sort"))
probe("cmd_parallel", ("north","east"),readchomp(combined),"east\nnorth")
subfolder = joinpath(folder,"work folder")
mkdir(subfolder)
probe("cmd_dir", basename(subfolder),basename(readchomp(Cmd(child("cwd");dir=subfolder))),"work folder")
keys = ("MBSKY_CMD_LABEL","MBSKY_CMD_CONTROL")
withenv(keys[1]=>"before",keys[2]=>"keep") do
replacement = Dict("SystemRoot"=>ENV["SystemRoot"],keys[1]=>"replace")
options = Cmd(child("env",keys...);env=replacement)
replace_cmd = setenv(child("env",keys...),replacement)
add_cmd = addenv(child("env",keys...),keys[1]=>"merge")
probe("cmd_env",keys,(readchomp(options),readchomp(replace_cmd),readchomp(add_cmd)),
("replace|missing","replace|missing","merge|keep"))
end
shell = Cmd([joinpath(ENV["SystemRoot"],"System32","cmd.exe"),"/d","/c","echo","local-exercise"])
probe("cmd_windows", "cmd.exe built-in echo", strip(read(shell,String)),"local-exercise")
sample = joinpath(folder,"sample.txt")
disposable = joinpath(folder,"delete.txt")
write(sample,"wave\n")
write(disposable,"discard")
write(joinpath(folder,"info.md"),"fixture")
only_text = sort(filter(name -> endswith(name,".txt") && isfile(joinpath(folder,name)), readdir(folder)))
probe("cmd_filter", "temporary files filtered by .txt suffix", only_text,
["child created.txt","delete.txt","sample.txt","sorted output.txt"])
probe("cmd_file_read",basename(sample),read(sample,String),"wave\n")
rm(disposable)
probe("cmd_file_rm",basename(disposable),ispath(disposable),false)
probe("cmd_file_mkdir",basename(subfolder),isdir(subfolder),true)
end
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录
入口文件:projects/julia-commands-performance/commands.jl
本地运行步骤快速上手
- 点击上方“保存源码”保存为
commands.jl,或下载右侧课程工程包。 - 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip
课程关联工程包(登记关联2章) · 含当前章节或实例 · mbskycom202612a206.zip