简体   繁体   English

如何将unix命令的输出传递给Julia脚本?

[英]How do I pipe the output of a unix command to a Julia script?

I'm trying to utilize the STDIN construct with Julia to read in the output of a unix command: 我正在尝试将STDIN构造与Julia一起使用以读取unix命令的输出:

#!/usr/bin/julia
readline(STDIN)

Then in my terminal: 然后在我的终端中:

$ cat myfile.txt | ./pipe.jl

but I am not getting any output. 但我没有任何输出。 What am I doing wrong? 我究竟做错了什么?

You're not doing anything with the result of readline(STDIN) . 您对readline(STDIN)的结果不做任何事情。 What kind of output do you want? 您想要什么样的输出?

Unlike the interactive REPL, a Julia script won't automatically print results. 与交互式REPL不同,Julia脚本不会自动打印结果。 You need to explicitly print the resulting string: 您需要显式print结果字符串:

print(readline(STDIN))

The line: 该行:

#!/usr/bin/julia

Has to be first, as Unix reads only the first line and checks it for #! 必须是第一个,因为Unix 读取第一行并检查其中的#! . Also, you need to do: 另外,您需要执行以下操作:

chmod +x pipe.jl

To make it work from ./pipe.jl construct. 要使其从./pipe.jl构造工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM