3

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)

6
  • Like that other guy said, you're ruling out the only ways you can readily communicate between shell scripts. That's fine; if you want to rule out how it can be done, you'll have to change your objectives so that it doesn't need to be done. If you want to do what you describe, you are limited to communicating via environment variables (to convey information into the other script without providing it all as command line arguments). If the invoked script must modify the calling scripts environment, you have no serious choices other than dotting (or 'sourcing') the other script. Commented Dec 28, 2015 at 0:04
  • You could consider having the first script exec the second, and then the second script exec a third — or a new interactive shell — and the new shell would inherit the environment from the first as modified by the second. But that's unlikely to be entirely satisfactory. Commented Dec 28, 2015 at 0:05
  • 1
    You could also consider using ini file to store the data. Commented Dec 28, 2015 at 0:29
  • The sourcing model would more likely be the second (variable using) script sourcing the first (variable defining) script. Since the first script would essentially be a "config" file for the second script. Commented Dec 28, 2015 at 1:18
  • @Lambic How would that work? I never even heard of using ini files for shell scripts. Commented Dec 28, 2015 at 16:57

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, that's unfortunate. Is there a way to work around it?
For scripts there's sourcing. Other than that, there are no good workarounds for environment variables. Choose another IPC mechanism
2

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!

1 Comment

Yeah that's the same intuition I had. My only concern is that it might be resources hungry, which can be an important issue on small low power devices.
0

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 i.e. 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 i.e. 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/' `dirname $0`/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.

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


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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.