简体   繁体   English

在shell / bash脚本中使用文件之间的变量

[英]Using variables between files in shell / bash scripting

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. 第二个脚本还需要能够更改第一个脚本中的变量。

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. 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) (PS。如果我误解了采购或出口是如何运作的,并且它适用于我的情况,请告诉我。我不会完全接受这些方法,在我读完之后,我只是不认为那些事情会做我想要的)

Environment variables are per process. 环境变量是每个进程。 One process can not modify the variables in another. 一个进程无法修改另一个进程中的变量。 What you're asking for is not possible. 你要求的是不可能的。

The usual workaround for scripts is sourcing, which works by running both scripts in the same shell process, but you say you don't want to do that. 脚本的常用解决方法是源代码,它通过在同一个shell进程中运行两个脚本来工作,但是你说你不想这样做。

I've also given this some thought. 我也有一些想法。 I would use files as variables. 我会使用文件作为变量。 For example in script 1 you use for writing variable values to files: 例如,在脚本1中,您用于将变量值写入文件:

echo $varnum1 > /home/username/scriptdir/vars/varnum1
echo $varnum2 > /home/username/scriptdir/vars/varnum2

And in script 2 you use for reading values from files back into variables: 在脚本2中,您可以将文件中的值读回变量:

$varnum1=$(cat /home/username/scriptdir/vars/varnum1)
$varnum2=$(cat /home/username/scriptdir/vars/varnum2)

Both scripts can read or write to the variables at any given time. 两个脚本都可以在任何给定时间读取或写入变量。 Theoretically two scripts can try to access the same file at the same time, I'm not sure what exactly would happen but since each file only contains one value, the time to read or write should be extremely short. 从理论上讲,两个脚本可以尝试同时访问同一个文件,我不确定究竟会发生什么,但由于每个文件只包含一个值,因此读取或写入的时间应该非常短。 In order to even reduce those times you can use a ramdisk. 为了减少那些时间你可以使用ramdisk。

I think this is much better than scripts editing each other (yuk!). 我认为这比编辑对方的脚本要好得多(yuk!)。 Live editing of scripts can mess up scripts and only works when you initiate the script again after the edit was made. 实时编辑脚本会使脚本陷入混乱只有在编辑完成后再次启动脚本时才有效。

Good luck! 祝好运!

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. 实际上,这很简单。

There are some prerequisites though. 但是有一些先决条件。

  1. The variable you want to set already has to exist in the file you're trying to set it in (I'm guessing the variable can be created as well when it doesn't exist yet, but that's not what I'm going for here). 你想要设置的变量必须存在于你试图设置它的文件中(我猜这个变量也可以在它不存在时创建,但那不是我想要的这里)。
  2. The file you're trying to set the variable in has to exist (obviously. I'm guessing again this can be done as well, but again, not what I'm going for). 你试图设置变量的文件必须存在(显然。我再次猜测也可以这样做,但同样,不是我想要的)。

Write

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

So ie setting the variable called Var1 to the value 5 , in the file test.ini : 因此,即在文件test.ini名为Var1的变量设置为值5
sudo sed -i 's/^\\(Var1=\\).*/\\15/' test.ini


Read

sudo grep -Po '(?<=VARNAME=).*' FILENAME

So ie reading the variable called Var1 from the file test.ini 因此,即从文件test.ini读取名为Var1的变量
sudo grep -Po '(?<=Var1=).*' test.ini


Just to be sure 只是要确定

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

So basically, just put `dirname $0`/ (including the backticks) in front of the filename. 所以基本上,只需将`dirname $0`/ (包括反引号)放在文件名前面。

The other option is to make `dirname $0`/ a variable (again including the backticks), which would look like this. 另一种选择是使`dirname $0`/变量(再次包括反引号),这看起来像这样。

my_dir=`dirname $0`

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

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 所以基本上,如果你有一个名为test.ini的文件,它包含这一行: Var1= (在我的测试中,变量可以开始为空,你仍然可以设置它。里程可能会有所不同。),你将能够设置并获取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. 我非常想知道如果这个解决方案,你们会怎么想。

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

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