Reputation: 164
my bash script consists of several functions and I have an option to start from different points in the script. Although you can only start from other steps than the first if a logfile exists. This logfile contains all given parameters during the runs and looks like this:
var1=ABC
var2=123
var3=456
var1=DEF
var4=XYZ
var2=789
As you can see it can happen that variables get different values assigned while the logfile is read. Now I would like to know how I can make my script read through this file at the beginning of my main function so that all following functions work with these variables from the logfile until the variables are changed within a function.
I don't want to assign new names for the variables as they are titled the same in the other functions. I actually just want the file to be read like its content would be written at the beginning of the main function.
function1 {
var1="X"
}
.
.
.
#MAIN
read logfile
function1
...
Upvotes: 0
Views: 193
Reputation: 7118
Run with a . name-of-the-script
.
This will pass the environment variables as set in child shell to parent shell.
Upvotes: 0
Reputation: 249103
If the file containing var1=ABC
etc. is called params.sh for example, you can do this in Bash:
source params.sh
This will behave as if you had pasted the entire contents of params.sh
at that location.
If you want to be compatible with other shells, this is equivalent, more portable, but less readable:
. params.sh
Upvotes: 2