附加到Scala Process的环境变量

时间:2014-09-24 02:44:11

标签: scala environment-variables

我找不到附加到环境变量的示例。例如,下面的代码完全取代PATH。有人可以直接或通过向我展示如何访问当前PATH来帮助我添加,以便我可以自行修改并将完全修改后的值传递给Process

val out2 = Process("env", None, "PATH" -> "/usr/local/bin")

1 个答案:

答案 0 :(得分:3)

有' S:

scala> util.Properties.envOrNone("PATH")
res0: Option[String] = Some(/home/apm/go1.1/go/bin:/home/apm/go/bin:/home/apm/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games)

那是:

scala> util.Properties.envOrSome("PATH", Some("")) map (p => s"$p${new sys.SystemProperties()("path.separator")}/tmp") get
warning: there was one feature warning; re-run with -feature for details
res5: String = /home/apm/go1.1/go/bin:/home/apm/go/bin:/home/apm/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/tmp

我刚注意到我的PATH已经过时了。

编辑成语:

scala> def appendToEnv(key: String, value: String) = util.Properties.envOrNone(key) match {
     |   case Some(v) if v.nonEmpty => s"$v${System getProperty "path.separator"}$value"
     |   case _ => value
     | }
appendToEnv: (key: String, value: String)String

scala> appendToEnv("PATH", "/opt")
res0: String = /home/apm/go1.1/go/bin:/home/apm/go/bin:/home/apm/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt

scala> appendToEnv("UNPATH", "/opt")
res1: String = /opt