【问题标题】:How can I handle multiple lines of output when running external processes with Shelly?使用 Shelly 运行外部进程时,如何处理多行输出?
【发布时间】:2018-03-28 08:15:56
【问题描述】:

我正在寻找Text -> [Text] 类型的函数,本质上它会根据换行符分割文本。但是我不能只在\n 上进行拆分,因为这可能会被转义,例如在\\n 中,我不确定还存在什么其他并发症。

Main.hs

{-# LANGUAGE OverloadedStrings #-}

import Data.Text (Text)
import Data.Text.IO
import Shelly

example :: Sh Text
example = run "./example.sh" []

main :: IO ()
main = do
  v <- shelly example
  Data.Text.IO.putStrLn v

example.sh

echo "test1\\n"
echo "test2"
echo "test3"

./example.sh的输出:

test1\n
test2
test3

所以这是 3 行的输出。

Shelly 是否支持某些功能来处理上述问题?或者也许是其他一些 Haskell 库来解析输出?我猜它看起来像putStrLn,但不是输出到IO (),而是输出到[Text]

【问题讨论】:

    标签: haskell shelly


    【解决方案1】:

    https://hackage.haskell.org/package/text-1.2.2.2/docs/Data-Text.html#v:lines

    main :: IO ()
    main = do
      v <- shelly example
      print $ Data.Text.lines v
    

    【讨论】:

      猜你喜欢
      • 2014-10-25
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多