我有关于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
感谢您的帮助。
答案 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
>>