如何编写在子流程上循环并处理其输出的惯用kotlin代码?

如何编写在子流程上循环并处理其输出的惯用kotlin代码?,kotlin,exec,Kotlin,Exec,我想编写一些基本上运行命令的kotlin代码: Runtime.getRuntime().exec("mycommand.sh") 但是,在这种情况下,mycommand.sh将永远不会退出。它会偶尔输出我想要处理的文本。假设输出如下所示: FOOBAR 1234 BARFOO 54657 ETCETC 9876 val inStream = BufferedReader(InputStreamReader(proc.inputStream)) val map = inStream.li

我想编写一些基本上运行命令的kotlin代码:

Runtime.getRuntime().exec("mycommand.sh")
但是,在这种情况下,
mycommand.sh
将永远不会退出。它会偶尔输出我想要处理的文本。假设输出如下所示:

FOOBAR 1234
BARFOO 54657
ETCETC 9876
 val inStream = BufferedReader(InputStreamReader(proc.inputStream))
 val map = inStream.lines()
               //maybe you need a more sufficient solution here
               .map { it.split(" ") } 
               .map { it[0] to it[1] }.toList()
假设第一行是5秒,第二行是10秒,第三行是15秒

我将如何编写代码,在每一行输入时接收并处理它? 例如,我可能想提取所有大写字母中的单词,并提取后面的数字,然后将这两段文本作为键值存储在哈希映射中


作为奖励,我想知道如何从kotlin程序中终止子进程(用SIGINT发出信号?)。

可能类似于:

FOOBAR 1234
BARFOO 54657
ETCETC 9876
 val inStream = BufferedReader(InputStreamReader(proc.inputStream))
 val map = inStream.lines()
               //maybe you need a more sufficient solution here
               .map { it.split(" ") } 
               .map { it[0] to it[1] }.toList()
这将产生一个
列表
,其中包含
s。
infix
方法
to
创建
s,这是简单的键值关联