如何确定正在运行的Haskell脚本或应用程序的目录? [英] How to determine the directory in which a running Haskell script or application lives?

查看:120
本文介绍了如何确定正在运行的Haskell脚本或应用程序的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Haskell脚本,它通过使用 runhaskell 实用程序的shebang行来运行。例如...

 #! / usr / bin / env runhaskell 
模块Main其中
main = do {...}

现在,我希望能够从脚本中自己确定脚本所在的目录 。因此,如果脚本存在于 /home/me/my-haskell-app/script.hs 中,我应该可以从任何地方运行它,使用相对或绝对它应该知道它位于 / home / me / my-haskell-app / 目录中。



我认为 System.Environment 模块中可用的功能可能会有所帮助,但效果会稍差。 getProgName 似乎没有提供有用的文件路径信息。我发现环境变量 _ (这是一个下划线)会包含脚本的路径,因为它被调用;但是,只要脚本通过其他程序或父脚本调用,该环境变量似乎就失去了它的价值(我需要从另一个父应用程序中调用我的Haskell脚本)。

同样有用的知识是我是否可以使用相同的技术或其他方法确定预编译的Haskell可执行文件所在的目录。

解决方案

有一个 FindBin 包,它似乎满足您的需求,也适用于编译的程序。


I have a Haskell script that runs via a shebang line making use of the runhaskell utility. E.g...

#! /usr/bin/env runhaskell
module Main where
main = do { ... }

Now, I'd like to be able to determine the directory in which that script resides from within the script, itself. So, if the script lives in /home/me/my-haskell-app/script.hs, I should be able to run it from anywhere, using a relative or absolute path, and it should know it's located in the /home/me/my-haskell-app/ directory.

I thought the functionality available in the System.Environment module might be able to help, but it fell a little short. getProgName did not seem to provide useful file-path information. I found that the environment variable _ (that's an underscore) would sometimes contain the path to the script, as it was invoked; however, as soon as the script is invoked via some other program or parent script, that environment variable seems to lose its value (and I am needing to invoke my Haskell script from another, parent application).

Also useful-to-know would be whether I can determine the directory in which a pre-compiled Haskell executable lives, using the same technique or otherwise.

解决方案

There is a FindBin package which seems to suit your needs and it also works for compiled programs.

这篇关于如何确定正在运行的Haskell脚本或应用程序的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆