简体   繁体   English

在ruby中设置环境变量并将其传递给shell

[英]set environment variable in ruby and pass it to shell

I am running a sinatra web app that gets a file (upload) from the user. 我正在运行一个从用户获取文件(上传)的sinatra Web应用程序。 It then transfers the file to a server via scp. 然后,它将文件通过scp传输到服务器。 Can I grab the filename from the params Hash and set it as an environment variable and pass it to the scp command. 我可以从params哈希中获取文件名并将其设置为环境变量,然后将其传递给scp命令。 Below is a snippet of my code. 以下是我的代码片段。

ENV[file] = params['arne_site_file'][:filename]
transfer = `scp -i /opt/zabbix-proxy/etc/.ssh/id_rsa $file zabbix@X.X.X.X:/home/zabbix/ARNE_5216_files`

Params Hash is - 参数哈希是-

{
"arne_site_file" => {
    :filename => "ARNE_SITE__BB5216_L16B_936441_HWY_54_A.xml"
},
        "submit" => "Validate File"
 }

Found the solution. 找到了解决方案。 It turns out I was missing quotes. 原来我缺少引号。 ENV['file'] = params['arne_site_file'][:filename]. ENV ['file'] = params ['arne_site_file'] [:filename]。 This resolved the issue. 这解决了问题。 Great to know that env variables can be set by Ruby for other bash scripts to access. 很高兴知道Ruby可以设置env变量供其他bash脚本访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM