Ruby:将环境变量传递给sudo脚本?

时间:2017-04-03 18:45:09

标签: ruby shell environment-variables environment

我有关于Shell out from ruby while setting an environment variable的后续问题。我尝试通过sudo将环境变量提供给shell脚本,但不成功。

这是示例单行脚本," /usr/bin/wrapper.sh"。

echo $MYVAR

测试,

$ irb
> system({'MYVAR' => "42"}, "echo $MYVAR")        ==> GOOD
> system({'MYVAR' => "42"}, "wrapper.sh")         ==> GOOD
> system({'MYVAR' => "42"}, "sudo echo $MYVAR")   ==> GOOD
> system({'MYVAR' => "42"}, "sudo wrapper.sh")    ==> NOT GOOD

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

你需要sudo -E

$ irb
>> system({'MYVAR' => "42"}, "sudo wrapper.sh")
=> true
>> system({'MYVAR' => "42"}, "wrapper.sh")
42
=> true
>> system({'MYVAR' => "42"}, "sudo -E wrapper.sh")
42
=> true
>>