从文件中设置环境变量 [英] Set environment variables from file
问题描述
我正在写在bash,它解析在某个文件夹3个变量文件的脚本,这是其中之一:
I'm writing a script in bash which parses files with 3 variables in a certain folder, this is one of them:
MINIENTREGA_FECHALIMITE="2011-03-31"
MINIENTREGA_FICHEROS="informe.txt programa.c"
MINIENTREGA_DESTINO="./destino/entrega-prac1"
此文件存储在./conf/prac1
This file is stored in ./conf/prac1
我的脚本minientrega.sh然后分析使用这种code中的文件:
My script minientrega.sh then parses the file using this code:
cat ./conf/$1 | while read line; do
export $line
done
但是,当我在命令行中执行 minientrega.sh prac1
不设置环境变量
我用源./conf/$1也试过
,但同样的问题仍然适用
I also tried using source ./conf/$1
but the same problem still applies
也许有一些其他的方式做到这一点,我只需要使用我通过我的脚本的参数文件的环境变量。
Maybe there is some other way to do this, I just need to use the environment variables of the file I pass as the argument of my script.
推荐答案
与方法的问题是在出口
,而
循环是发生在一个子壳,而这些变量不会在当前的shell(while循环父shell)提供。
Problem with your approach is the export
in the while
loop is happening in a sub shell, and those variable will not be available in current shell (parent shell of while loop).
文件本身添加导出
命令:
export MINIENTREGA_FECHALIMITE="2011-03-31"
export MINIENTREGA_FICHEROS="informe.txt programa.c"
export MINIENTREGA_DESTINO="./destino/entrega-prac1"
然后,你需要使用文件中源当前的shell:
Then you need to source in the file in current shell using:
. ./conf/prac1
或
source ./conf/prac1
这篇关于从文件中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!