最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Updating value of a variable in shell script from another file and export - Stack Overflow

programmeradmin4浏览0评论

I have a properties file with below values:

value1=one
value2=two
value3=three
flag=true

Currently my code loops through them and exports the data

   for var in `grep -v '#' $properties_file| grep '=' |cut -d '=' -f 1`; do
export $var;

I want to override the value of value3 to five which I get from another file updateValue.properties if flag is set to true in my current properties file and then export it as $var with the others. How do I go about it?

updateValue.properties has value:

value3=five

Thanks!

I have a properties file with below values:

value1=one
value2=two
value3=three
flag=true

Currently my code loops through them and exports the data

   for var in `grep -v '#' $properties_file| grep '=' |cut -d '=' -f 1`; do
export $var;

I want to override the value of value3 to five which I get from another file updateValue.properties if flag is set to true in my current properties file and then export it as $var with the others. How do I go about it?

updateValue.properties has value:

value3=five

Thanks!

Share Improve this question asked Feb 18 at 3:58 CleanSockCleanSock 3832 gold badges4 silver badges15 bronze badges 1
  • Maybe you could find a way at bash background process modify global variable – F. Hauri - Give Up GitHub Commented 2 days ago
Add a comment  | 

2 Answers 2

Reset to default 0

Use source:

$ cat vars
var1=1
var2=2
var3=3

$ source vars
$ echo $var1 $var2 $var3
1 2 3

$ cat override 
var2=5
$ source override
$ echo $var1 $var2 $var3
1 5 3

Assuming that your file stores the name of the variable:

value3=$(cat file_with_contennt_for_this_variable)

Be careful, in that this would put the whole content of the file into your variable, including empty lines etc.

发布评论

评论列表(0)

  1. 暂无评论