2017-09-17 49 views
0

我在〜/ .bashrc中保存了一些ENV,我关闭并重新打开了该文件,并且它们确实存在。Rails应用程序不会读取.bashrc中的env变量

的.bashrc:

# ~/.bashrc: executed by bash(1) for non-login shells. 
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) 
# for examples 

. /etc/apache2/envvars 

# If not running interactively, don't do anything else 
[ -z "$PS1" ] && return 

# don't put duplicate lines or lines starting with space in the history. 
# See bash(1) for more options 
HISTCONTROL=ignoreboth 

# append to the history file, don't overwrite it 
shopt -s histappend 

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1) 
HISTSIZE=1000 
HISTFILESIZE=2000 

# check the window size after each command and, if necessary, 
# update the values of LINES and COLUMNS. 
shopt -s checkwinsize 

# make less more friendly for non-text input files, see lesspipe(1) 
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" 

# Alias definitions. 
# You may want to put all your additions into a separate file like 
# ~/.bash_aliases, instead of adding them here directly. 
# See /usr/share/doc/bash-doc/examples in the bash-doc package. 

if [ -f ~/.bash_aliases ]; then 
    . ~/.bash_aliases 
fi 

# enable programmable completion features (you don't need to enable 
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile 
# sources /etc/bash.bashrc). 
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then 
    . /etc/bash_completion 
fi 

PS1='\[\033[01;32m\]${C9_USER}\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 " (%s)") $ ' 

# If this is an xterm set the title to [email protected]:dir 
case "$TERM" in 
xterm*|rxvt*) 
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\[email protected]\h: \w\a\]$PS1" 
    ;; 
*) 
    ;; 
esac 

export rvm_silence_path_mismatch_check_flag=1 

export TWILIO_SID="AXXXXX81b7eb5aXXXXeXXX6c9bfecX6X" 
export TWILIO_TOKEN="XXX87XXXXXXe650ff2XXXXfXXXXX8X2" 
export TWILIO_PHONE_NUMBER="+147043XXXX" 

然而,当我告诉我的Rails应用程序在我的控制器使用它们,例如使用ENV [ “TWILIO_SID”],它不知道他们。我尝试用bash回显它们,它只是一个空行,在HTML中也是空行。 我正在使用c9云IDE,并且有一个选项可以将ENV手动输入到Rails shell中,当我这样做时,一切正常。但我的任务要求bashrc文件...为什么既不bash也不轨道终端阅读我的.bashrc?任何帮助?

PS:总体目标是在我的Rails应用程序中设置一个UNIX env变量。我不能使用菲格罗。

PS2:这里是我使用变量的控制器中的代码。当我对它们进行硬编码时,一切正常,所以我知道env变量有什么问题。

require 'twilio-ruby' 

def index 
end 

class TexterController < ApplicationController 
    def index 
    end 

    def text 
    @number = params[:number] 
    @message = params[:message] 

    twilio_sid = ENV["TWILIO_SID"] 
    twilio_token = ENV["TWILIO_TOKEN"] 
    twilio_phone_number = ENV["TWILIO_PHONE_NUMBER"] 

    @client = Twilio::REST::Client.new(twilio_sid, twilio_token) 

    @message = @client.messages.create(
     to: @number, 
     from: twilio_phone_number, 
     body: @message 
    ) 

     render "pages/text.html.erb" 
    end 
end 
+0

'source〜/ .bashrc'? – Cyrus

+0

试过了,没有工作:/ –

+0

请加'export -p | grep“TWILIO”'和你的ruby代码到你的问题。 – Cyrus

回答

0

找到它了。 原因是某些原因,即使当我输入.bashrc时,env变量也不会被读取,但是当我将它们添加到.profile时,它们被读取并且一切正常。 甚至尝试将它们添加到.bashrc,然后source ~/.bashrc到.profile,但仍然无法正常工作。

1

这里是原因:

要更改环境变量“永久”你需要至少考虑下列情形:

  • 登录/非登录shell
  • 互动/非交互式壳

bash作为登录shell将在顺序加载/etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile

bash作为非登录交互式外壳将加载~/.bashrc

bash作为非登录非交互壳将加载在环境变量$ BASH_ENV

源指定的配置:how to permanently set environmental variables

相关问题