如何使用 Ruby 永久导出环境变量? [英] How to export environment variable permanently using Ruby?

查看:23
本文介绍了如何使用 Ruby 永久导出环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Ruby 新手,想将环境变量永久导出到主机系统.

I am newbie in Ruby and want to export environment variable permanently to host system.

现在我正在使用以下 ruby​​ 脚本.在此脚本中,有一个 ENV 命令将 URL env var 添加到当前执行环境中,并且工作正常.但是我想让这个改变持久化到主机,在我的例子中是它的 linux.我该怎么做?

Right now I have following ruby script on which I am working. In this script there is ENV command that adds URL env var to current execution env and it works fine. But I want to make this change persistent to hostmachine, in my case its linux. how can I do that?

require 'singleton'

java_import com.test.TestClass;
class RubyClient
  include Singleton

 def initialize
  ENV['URL'] = 'http://localhost'
 end
end

def test_client
  puts TestClass.getEnv
end

class TestClass{
 public String getEnv({
  return StringUtils.trimToNull(System.getenv("URL"));
 }
}

推荐答案

这是不可能的.环境变量适用于当前运行的进程,适用于当前进程.由该进程启动的子进程最初将继承其父进程的环境,但可以(并且经常这样做)用自己的环境覆盖它.

This is impossible. Environment variables apply to the currently running process, and only to the currently process. Child processes started by this process will initially inherit their parent's environment, but can (and often do) override it with its own.

一个进程不能改变另一个进程的环境.这与 Ruby 或 Java 无关,这是所有 Unices 以及许多其他具有类似环境"概念的操作系统的基本安全属性.

One process cannot change another process's environment. This has nothing to do with Ruby or Java, this is a fundamental security property of all Unices as well as many other OSs that have a comparable concept of "environment".

这篇关于如何使用 Ruby 永久导出环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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