利用壳文件之间的变量/ bash脚本编程 [英] Using variables between files in shell / bash scripting

查看:127
本文介绍了利用壳文件之间的变量/ bash脚本编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题已经张贴在这里很多次,但它似乎永远不会回答我的问题。

This question has been posted here many times, but it never seems to answer my question.

我有两个脚本。第一个包含一个或多个变量,所述第二脚本需要那些变量。所述第二脚本还需要能够改变在所述第一脚本中的变量。

I have two scripts. The first one contains one or multiple variables, the second script needs those variables. The second script also needs to be able to change the variables in the first script.

我在采购(其中包含变量的第一脚本运行第二个脚本)不感兴趣或(使用环境变量)出口。我只是单纯的想确保第二个脚本可以读取和改变(get和set)的变量可以在第一个脚本。

I'm not interested in sourcing (where the first script containing the variables runs the second script) or exporting (using environment variables). I just simply want to make sure that the second script can read and change (get and set) the variables available in the first script.

(PS。如果我误解采购或出口是如何工作的,以及它适用于我的方案,请让我知道,我不是完全封闭的这些方法,我读过之后,我不认为这些东西会做我想做的)

(PS. If I misunderstood how sourcing or exporting works, and it applies to my scenario, please let me know. I'm not completely closed to those methods, after what I've read, I just don't think those things will do what I want)

推荐答案

所以,在网络上寻找长期和大量的努力后,我终于找到某种解决方案。其实,这是很简单的。

So after a long search on the web and a lot of trying, I finally found some kind of a solution. Actually, it's quite simple.

有一些prerequisites虽然。

There are some prerequisites though.


  1. 您要设置已变量有你试图将其设置为(我猜的变​​量可以被创建,以及当不存在,就在文件中存在,但是这不是我要去这里)。

  2. 您正在尝试设置变量中有存在的文件(很明显。我猜这同样可以做到为好,但同样,不是我要的)。

sudo的桑达-iS / ^ \\(VARNAME = \\)。* / \\ 1VALUE /文件名

所以,即所谓的 VAR1 变量的值设置为 5 ,文件中
test.ini :结果
sudo的桑达-iS / ^ \\(VAR1 = \\)。* / \\ 15 /'test.ini

So i.e. setting the variable called Var1 to the value 5, in the file test.ini:
sudo sed -i 's/^\(Var1=\).*/\15/' test.ini

须藤的grep -Po(小于?= = VARNAME)。*文件名

所以,即所谓的读 VAR1变量从文件 test.ini 结果
须藤的grep -Po(小于?= VAR1 =)。*test.ini

So i.e. reading the variable called Var1 from the file test.ini
sudo grep -Po '(?<=Var1=).*' test.ini

运行,设置从不同的文件夹一进入你的脚本所在变量的脚本时,我注意到一些问题。
为了确保这总是正确的,你可以做两件事情之一:

I've noticed some issues when running the script that sets variables from a different folder than the one where your script is located. To make sure this always go right, you can do one of two things:

sudo sed -i 's/^\(VARNAME=\).*/\1VALUE/' `dirname $0`/FILENAME

所以基本上,只要把`$目录名称0` / (包括反引号)在文件名前。

So basically, just put `dirname $0`/ (including the backticks) in front of the filename.

另一个选择是让`$目录名称0` / 的变量(包括再次反引号),这将是这样的。

The other option is to make `dirname $0`/ a variable (again including the backticks), which would look like this.

my_dir=`dirname $0`

sudo sed -i 's/^\(VARNAME=\).*/\1VALUE/' $my_dir/FILENAME


所以基本上,如果你有一个名为 test.ini 文件,其中包含此行: VAR1 = (在我的测试中,变量可以开始空,您仍然可以进行设定。里程可能会有所不同。),你就可以设置并获得 VAR1


So basically, if you've got a file named test.ini, which contains this line: Var1= (In my tests, the variable can start empty, and you will still be able to set it. Mileage may vary.), you will be able to set and get the value for Var1

我可以证实,这个工程(对我来说),但因为你的一切,在脚本的方式更多的经验,然后我的,没拿出这个,我猜这是不是一个伟大的方式来做到这一点。

I can confirm that this works (for me), but since you all, with way more experience in scripting then me, didn't come up with this, I'm guessing this is not a great way to do it.

另外,我不能告诉你什么是发生以上这些命令的第一件事情,我只知道他们的工作。

Also, I couldn't tell you the first thing about what's happening in those commands above, I only know they work.

所以,如果我做一些愚蠢的事,或者如果你能向我解释发生的事情在上面的命令,请让我知道。我很好奇,想看看你们的想法,如果这个解决方案。

So if I'm doing something stupid, or if you can explain to me what's happening in the commands above, please let me know. I'm very curious to find out what you guys think if this solution.

这篇关于利用壳文件之间的变量/ bash脚本编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆