Ruby on rails Rails应用程序不读取.bashrc中的环境变量

Ruby on rails Rails应用程序不读取.bashrc中的环境变量,ruby-on-rails,bash,environment-variables,Ruby On Rails,Bash,Environment Variables,我在~/.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

我在~/.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 user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\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输入RailsShell,当我这样做时,一切正常。但我的作业要求提供bashrc文件。。。为什么bash和rails终端都不读取my.bashrc?有什么帮助吗

PS:总体目标是在我的Rails应用程序中设置一个UNIX环境变量。我不能用费加罗

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
需要“twilio ruby”
def索引
结束
类TexterController<应用程序控制器
def索引
结束
定义文本
@编号=参数[:编号]
@消息=参数[:消息]
twilio_sid=ENV[“twilio_sid”]
twilio_令牌=环境[“twilio_令牌”]
twilio_电话号码=ENV[“twilio_电话号码”]
@client=Twilio::REST::client.new(Twilio\u sid,Twilio\u令牌)
@message=@client.messages.create(
致:@number,
发件人:twilio_电话号码,
正文:@消息
)
呈现“pages/text.html.erb”
结束
结束
找到了它。 事实证明,出于某种原因,即使在我使用source.bashrc时,env变量也不会被读取,但当我将它们添加到.profile时,它们会被读取,并且一切正常。
甚至尝试将它们添加到.bashrc,然后将
源代码~/.bashrc
添加到.profile,但仍然不起作用。

原因如下:

为了改变环境变量“永久性”,你至少需要考虑这些情况:

  • 登录/非登录shell
  • 交互式/非交互式shell
Bash作为登录shell将按照以下顺序加载
/etc/profile、~/.Bash\u profile、~/.Bash\u login、~/.profile

Bash作为非登录交互shell将加载
~/.bashrc

Bash作为非登录非交互式shell将加载环境变量$Bash_ENV


来源:

Source~/.bashrc
?已经尝试过了,但没有成功:/请将
export-p | grep“TWILIO”
的输出和您的ruby代码添加到您的问题中。
export-p | grep“TWILIO”>output.txt
生成一个空的txt文件。不过,我确实在问题中添加了代码。请在问题中添加您的~/.bashrc。