【问题标题】:Rails app does not read env variables in .bashrcRails 应用程序不读取 .bashrc 中的环境变量
【发布时间】:2018-02-26 09:55:09
【问题描述】:

我在 ~/.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 输入到 Rails shell 中,当我这样做时,一切正常。但是我的任务要求提供 bashrc 文件...为什么 bash 和 rails 终端都没有读取我的 .bashrc?有什么帮助吗?

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

PS2:这是我使用变量的控制器中的代码。当我对它们进行硬编码时,一切正常,所以我知道环境变量发生了一些事情。

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

【问题讨论】:

  • source ~/.bashrc?
  • 已经试过了,还是不行:/
  • 请将export -p | grep "TWILIO" 的输出和您的红宝石代码添加到您的问题中。
  • export -p | grep "TWILIO" &gt; output.txt 生成一个空的 txt 文件。不过,我确实将我的代码添加到了问题中。
  • 请将您的 ~/.bashrc 添加到您的问题中。

标签: ruby-on-rails bash environment-variables


【解决方案1】:

原因如下:

要“永久”更改环境变量,您至少需要考虑以下情况:

  • 登录/非登录外壳
  • 交互式/非交互式外壳

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

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

Bash 作为非登录非交互式 shell 将加载环境变量中指定的配置 $BASH_ENV

来源:how to permanently set environmental variables

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 2021-03-12
      • 2011-01-16
      • 2019-08-13
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      相关资源
      最近更新 更多