简体   繁体   中英

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)

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.

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:

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:

$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.

I think this is much better than scripts editing each other (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 :
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
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/' FILENAME

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

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



sudo sed -i 's/^\(VARNAME=\).*/\1VALUE/' 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


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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