How do I get a variable's value from one script and import it in another script? But the catch is that the script where I am going to get the variable's value has also some commands that I don't want to run:
Let's say this is script1.sh:
variable="Hello"
#some command here e.g. yum check-update
And in script2.sh:
echo $var
I've found an approach where one uses the EXPORT command and puts ./script1.sh inside the script2.sh but it runs the other commands.
So, how can I get the variable's value without running the commands inside it? I hope this question is clear. Thanks in advance!
Best Answer
To summarize, you want to retrieve a value out of
script1.sh
without running all the commands inscript1.sh
. I will assume thatscript1.sh
is not under your control, possibly because it is vendor-provided, or managed by an obstinate colleague, or similar such issue, which makes it so thatscript1.sh
has to be used as is.I will present two approaches. The first is good for getting specific variables out of
script1.sh
, one at time:This uses
awk
to read (not execute, just read)script1.sh
.awk
looks for the line that begins withvariable=
and then writes what appears after the double-quote mark on that line. The output fromawk
is capture into variablevar
.In more detail:
The statement is of the form
var=$(...)
. This means that whatever is in the parentheses is run as a bash command and its standard output is assigned to the variablevar
.Inside the parentheses, we have the
awk
command:awk -F'"' '/^variable=/ {print $2}' script1.sh
. Let us consider it in parts.awk
breaks lines (records) into fields.-F'"'
tells awk to use double-quote as a field separator./^variable=/
tellsawk
to restrict operation to lines that begin withvariable=
. Thus, all the miscellaneous commands inscript1.sh
will be ignored.{print $2}
tellsawk
to print the second field. This means whatever is between the first and second occurence of a double-quote character.The last argument to
awk
tells it what file to read:script1.sh
.The above approach is good for handling one variable at a time and allows you to rename the variable if you like.
How to handle many variables
If you want to source all the variables assigned in
script1.sh
, consider:This uses
grep
to extract all lines fromscript1.sh
that look like variable assignments. These lines are then run in the current shell, assigning the variables.If you use this approach first check to make sure that you want all the variables and that there aren't any that will interfere with what you are doing. If there are such, we can exclude them with a second
grep
command.Considering the pieces in turn:
source file
tells the shell to execute thefile
in the current shell.<(...)
is called process substitution. It allows us to use the output of a command in place of a file name.The command
grep -E '^\w+=' script1.sh
extracts all lines that seem like variable assignments. If you run this command by itself on the command line, you should see something like:and so on. You should do this first and inspect the output to make sure that these are the lines that you want to execute.