简体   繁体   English

Windows中的Haskell runCommand

[英]Haskell runCommand in Windows

As a simple exercise, I'm trying to change the prompt in Win7's Command Prompt window with the following little program: 作为一个简单的练习,我试图使用以下小程序在Win7的命令提示符窗口中更改提示:

module Main where
 import System.Environment
 import System.Process 

 p :: String -> String 
 p name = "Prompt " ++ name ++ "\r\n"

 main :: IO ()
 main = do
     putStrLn ("Give me a name:")
     name <- getLine
     putStrLn (p name)
     pid <- runCommand $ p name

Although it runs fine in the Command Prompt window, it does not actually change the prompt. 虽然它在命令提示符窗口中运行正常,但它实际上并不会更改提示。 Entering the same command on the command line by hand does change it. 手动在命令行上输入相同的命令会更改它。 When I use "system" (which returns an exit code) rather than "runCommand" (which just returns a pid) it gives "ExitSuccess", but still doesn't change the prompt. 当我使用“system”(返回退出代码)而不是“runCommand”(它只返回一个pid)时,它会给出“ExitSuccess”,但仍然不会改变提示。

This isn't a Haskell issue per-se, just that you can't easily write a program in any language that will change the environment of its parent process. 这本身不是Haskell问题,只是你不能轻易地用任何会改变其父进程环境的语言编写程序。

In your scenario the prompt is controlled by the PROMPT environment variable, and the "parent process" is the Command Prompt ( cmd.exe ) that you launched your Haskell program from. 在您的方案中,提示由PROMPT环境变量控制,“父进程”是您从中启动Haskell程序的命令提示符( cmd.exe )。

I would suggest that instead of trying to alter the parent cmd.exe process, you spawn a new cmd.exe (also with runCommand ) after you have changed the prompt. 我建议您更改提示后,不要尝试更改父cmd.exe进程,而是生成新的cmd.exe (也使用runCommand )。 You should wait for this process to finish with waitForProcess on the PID returned from runCommand , otherwise your Haskell program will exit with the child shell still running. 您应该等待此过程在runCommand返回的PID上使用waitForProcess runCommand ,否则您的Haskell程序将在子shell仍在运行时退出。

You will probably also have to switch from running the Prompt command to actually changing the PROMPT environment variable directly in your Haskell process, because what actually happens when you run the Prompt command as above is that a new cmd.exe is started just to run that command, so the environment change is immediately thrown away. 您可能还必须从运行Prompt命令切换到直接在Haskell进程中实际更改PROMPT环境变量,因为当您运行上述Prompt命令时实际发生的情况是启动新的cmd.exe只是为了运行命令,所以环境变化立即被扔掉。

You can edit the environment with the setenv package . 您可以使用setenv包编辑环境。

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

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