我正在使用scotty,无论如何尝试从POST请求执行shell脚本,都会得到相同的类型错误。
main = scotty 3000 $ do
post "MyPage/ScriptTime/run" $ do
aparameter <- param "aparameter"
bparameter <- param "bparameter"
cparameter <- param "cparameter"
dparameter <- param "dparameter"
eparameter <- param "eparameter"
rawSystem "./shellscript.sh" [ "-a"
, aparameter
, "-b"
, bparameter
, "-c"
, cparameter
, dparameter
, eparameter
]在可以从haskell程序调用bash或shell脚本吗?和在Haskell中执行系统命令上使用答案无助于更改错误消息。
该错误如下:
Main.hs:68:5: error:
• Couldn't match type ‘IO’
with ‘Web.Scotty.Internal.Types.ActionT
Data.Text.Internal.Lazy.Text IO’
Expected type: Web.Scotty.Internal.Types.ActionT
Data.Text.Internal.Lazy.Text IO ()
Actual type: IO GHC.IO.Exception.ExitCode
• In a stmt of a 'do' block:
rawSystem "./shellscript.sh" ["-a", aparameter, "-b", bparameter, ....]
In the second argument of ‘($)’, namely
‘do aparameter <- param "aparameter"
bparameter <- param "bparameter"
cparameter <- param "cparameter"
dparameter <- param "dparameter"
....’我已经更改了用于调用shell脚本的函数和库的几种方式,包括:
() <- createProcess (proc "./shellscript.sh" ["-a", aparameter, "-b", bparameter, ...])
runProcess (shell "./shellscript.sh -a aparameter -b bparameter ...") >>= print
我尝试过使用System.Process、System.Process.Typed和System.Cmd库。
有人能帮我理解我的类型错配吗。
发布于 2019-12-05 16:58:37
不匹配发生在函数运行的单变量中。rawSystem运行在IO monad中,如其类型所示,以及在错误消息中。但是scotty的run函数需要运行在蒙纳德中的东西。
错误消息告诉您的是:无法匹配类型‘IO _ _with_ _ActionT文本IO’。
因为ActionT有一个MonadTrans实例,所以您只需将一个IO函数lift为ActionT _ IO
lift $ rawSystem "./shellscript.sh" [...]在这样做之后,下一个问题将是返回类型:rawSystem返回一个ExitCode,而scotty的run函数需要单元。如果您可以丢弃退出代码(尽管我没有重新注释它),您可以将它绑定到一个未命名的变量:
_ <- lift $ rawSystem ...
pure ()或者,更好的是,您可以使用void丢弃该值:
void . lift $ rawSystem ...https://stackoverflow.com/questions/59199528
复制相似问题