简体   繁体   English

在IntelliJ IDEA Bash脚本中使用环境变量

[英]Use Environment Variables in IntelliJ IDEA Bash Script

Goal: to run a bash script as the method for builds/runs, by building a runConfiguration that calls the bash script. 目标:通过构建调用bash脚本的runConfiguration来运行bash脚本作为构建/运行方法。

I'm using the BashSupport plugin in the latest version of IntelliJ IDEA. 我正在最新版本的IntelliJ IDEA中使用BashSupport插件。 I am trying to reference system environment variables from a shell script that is called from a runConfiguration, but I can't pull in system environment variables, unless I manually specify the environment variables in the runConfiguration's XML, which is not an option. 我试图从从runConfiguration调用的shell脚本中引用系统环境变量,但是除非我在runConfiguration的XML中手动指定环境变量,否则就不能提取系统环境变量,这是不可行的。 I want to be able to pull variables directly from the system. 我希望能够直接从系统中提取变量。 I have tried putting environment variables in /etc/environment and .bashrc and .zshrc. 我尝试将环境变量放在/ etc / environment和.bashrc和.zshrc中。

Any advice would be greatly appreciated! 任何建议将不胜感激!

OS: Ubuntu-based 操作系统:基于Ubuntu

Thank you @CrazyCoder! 谢谢@CrazyCoder! That was definitely it. 就是这样。 Turns out that after a system restart (not just an IntelliJ restart), IntelliJ was able to pick up the variables I put in /etc/environment . 事实证明,在系统重新启动(不仅是IntelliJ重新启动)之后,IntelliJ能够提取我在/etc/environment输入的变量。 Didn't even need to add them to ~/.profile . 甚至不需要将它们添加到~/.profile Thanks for doing the research! 感谢您的研究!

Another solution: 另一个解决方案:

  1. Keep your variables in ~/.bashrc 将变量保存在〜/ .bashrc中
  2. Create a Run/Debug Configuration using BashSupport plugin 使用BashSupport插件创建运行/调试配置
  3. Specify -i in Interpreter options, because if you don't, it will exit ~/.bashrc prematurely 在解释器选项中指定-i ,因为如果不这样做,它将过早退出〜/ .bashrc

@See .bashrc file @See .bashrc文件

# If not running interactively, don't do anything
case $- in
  *i*) ;;
  *) return;;
esac
#....

It is troublesome to set environment variables from the Edit Configuration of Intelli-J editing. 从Intelli-J编辑的“编辑配置”中设置环境变量很麻烦。 I would like to read it from a file if possible. 如果可能的话,我想从文件中读取它。

So I did it like this. 所以我这样做了。

  1. Write environment variables in yaml file. 在yaml文件中写入环境变量。
  2. Set the full path of the yaml file to the environment variable of Intell-J's Edit Configuration. 将yaml文件的完整路径设置为Intell-J的“编辑配置”的环境变量。
  3. Read the yaml file from program. 从程序中读取yaml文件。
  4. Set environment variables programatically. 以编程方式设置环境变量。

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

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