Erlang:如何将文件中的stdin输入管理到erlang程序并匹配eof? [英] Erlang: How to pipe stdin input from a file to an erlang program and match eof?
问题描述
我有一个文件 hr.erl
,我从shell编译它。有一个函数,它使用 io:fread()
接受来自stdin的输入。我写了一个案件表达式与守卫,如果它匹配 {ok,[0]}
它应该终止。而不是 0
,我实际上需要它是 eof
。
- 如何在shell中运行eof?
- 我有一个文件
inp.txt
,值为1
<$ c每一行$ c> 23
和0
如何使用<
管道运算符传递它?有什么像erl -hr< inp.txt
?我可以将它管理到shell中的stdin。
这是我的程序到目前为止(更新为包括 eof
)。
-module(hr)。
-export([main / 0])。
r(L) - >
case io:fread(,〜d)
eof - >
io:format(eof〜n,[]),
ok;
{ok,[0]} - >
io:format(terminate〜n,[]),
列表:reverse(L);
{ok,[N]} - >
io:format(inp〜p〜n,[N]),
r([N | L])
end。
main() - > R([])。
从shell
code> 1> C(小时)。
{ok,hr}
2> HR:main()中。
1
inp 1
2
inp 2
3
inp 3
0
终止
[1, 2,3]
谢谢。
我可以使用 escript 一>。使用 main(_)
函数,即 escript
兼容方式编写没有模块或导出信息的erlang程序。然后我们可以使用 cat
管道输入,如
cat inp.txt | escript hr.erl
这个工作和程序在遇到 eof
。但是我仍然不知道为什么在使用重定向操作符
时不起作用。
How to pipe input from a file as stdin to an erlang program running in the shell as well as standalone?
I have a file hr.erl
and I compile it from the shell. There is a function in it which accepts input from stdin using io:fread()
. I have written a case expression with guards where if it matches {ok, [0]}
it should terminate. Instead of 0
, I actually need it to be eof
.
- How to send eof when running in a shell?
- I have a file
inp.txt
with values1
2
3
and0
on each line. How can I pass it using the<
pipe operator? Is there anything likeerl -hr <inp.txt
? Can I pipe it to stdin within the shell.
Here is my program so far (updated to include eof
).
-module(hr).
-export([main/0]).
r(L) ->
case io:fread("", "~d") of
eof ->
io:format("eof~n", []),
ok;
{ok, [0]} ->
io:format("terminate~n", []),
lists:reverse(L);
{ok, [N]} ->
io:format("inp ~p~n", [N]),
r([N|L])
end.
main() -> r([]).
From shell
1> c(hr).
{ok,hr}
2> hr:main().
1
inp 1
2
inp 2
3
inp 3
0
terminate
[1,2,3]
Thanks.
I am able to pipe input when using escript. Write the erlang program without module or export info, with main(_)
function, i.e., in escript
compatible way. Then we can pipe input using cat
like
cat inp.txt | escript hr.erl
This works and program terminates when it encounters eof
. But I still don't know why it's not working when using redirect operator <
.
这篇关于Erlang:如何将文件中的stdin输入管理到erlang程序并匹配eof?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!