How to pass output of one command as env variables to another - bash

I have a bash script that when ran, produces output like this:
VAR1=test
VAR2=test
I want to pass these variables as environment variables to an npm script, which in this case is just running mocha in the current directory, like if I was running VAR1=test VAR2=test mocha.
Thanks in advance.

It's not a great design for a bash script to output shell variable assignments like that, but you can work around it with a wrapper script runwithvars:
#!/bin/bash
set -a # Auto-export all new variables
eval "$(mybashscript)" # Perform whichever actions the script outputs
exec "$#" # Execute the specified command
Now you can use runwithvars mocha to run mocha with those variables.
Note that if the script outputs key-value pairs instead of shell variable assignments, e.g. VAR1=some value with spaces instead of VAR1='some value with spaces', then this answer does not apply and could be fragile or dangerous.

Use export:
export VAR1=test
export VAR2=test

Related

Set env var in bash script

I want to set 2 temp env vars and then run a binary.
The command is like this:
ENV_1=firstparam ENV_2=secondparam my_binary
I want to move the 2 env var assignment in a bash script and use a command like this:
setparams.sh my_binary
setparams.sh
#!/bin/bash
ENV_1=firstparam
ENV_2=secondparam
What's wrong here? Why do the vars are not being set?
By default all user defined variables are local. They are not exported to new processes. Use export command to export variables and functions
export ENV_1=firstparam
export ENV_2=secondparam
Also, instead of executing you should call source (built-in command that executes the content of the file passed as argument, in the current shell):
source setparams.sh && my_binary
Since you're passing my_binary as an argument to the script, I assume you want the script to (a) set the environment variables and then (b) invoke the command you sent it.
One way to do that is:
#!/bin/bash
ENV_1=firstparam ENV_2=secondparam "$#"
"$#" expands to the list of arguments you passed to the script.
If you set variables like that, they'll be inherited in the environment of any command you run on the same command line, but not by any subsequent commands.
If you wanted to execute more than one command with those environment variables, you could do:
#!/bin/bash
export ENV_1=firstparam
export ENV_2=secondparam
some_command
some_other_command
Then $ENV_1 and $ENV_2 will appear in the environment of some_command and some_other_command -- but not in the environment of your shell after set_params.sh finishes.
If you want a script to set environment variables that will be available in your interactive shell, you'll have to invoke it with . ./set_params.sh or source ./set_params.sh. (And in that case you don't need the #!/bin/bash at the top, since it will execute in your current shell.)
You need to export your environment variables:
setparams.sh
#!/usr/bin/env bash
# Export environment variables
export ENV_1=firstparam
export ENV_2=secondparam
# Launch binary with its provided arguments
"$#"
Testing it:
./setparams.sh bash -c 'echo "$ENV_1"'
Output:
firstparam

How to have a Gradle task set shell set environment variables?

I have a Gradle task which invokes a shell script which sets some environment variables. These variables are used by another shell script which is also invoked by Gradle as another task. However, the second task cannot view these variables. What is wrong and how do I actually achieve my required behavior?
I am working in Unix/Linux. Both the Gradle tasks are basically Exec type of tasks.
Assuming, I am ready to out source the work done by the first shell script (setting env variables) to Gradle. How can I make the second shell script pick up these variables?
Are you exporting environ variables you want to share? Try with export keyword, as suggested in the following example:
$ cat a.sh
#! /bin/sh
export VAR=val
/path/to/b.sh
$ cat b.sh
#! /bin/sh
echo $VAR
When you execute a.sh the output "val" is printed on the screen.
Hope this helps! :)

How to export or set a make variable from a shell file

I am running a shell script from make environment
I execute the script with input parameters as make variables:
/shell_script.sh $(make_var1) $(make_var2)
I process these variables in shell. I want to assign the result from a shell command to the make variable and export back to shell.
make_var=shell_command
How can I do this?
It is not trivial to change the parent environment of a shell-script, one approach is to echo the export statements and source the output of the script in your parent environment:
...
echo "export make_var1=${make_var1}"
...
and when you launch your script do it using eval:
eval $(./shell_script.sh $make_var1 $make_var2)
this is the approach taken by for example ssh-agent.
A second option is to source the script, in that case the script will be run line-by-line in the current shell:
. shell-script.sh
any export statements in the script will be run in the current shell.

setting bash variables while executing scripts

My sample script looks like
#!/bin/bash
VAR="foo"
if [ $VAR etc etc
then etc
...
Is there any way to call the script changing the value of $VAR without writing the script?
I would imagine something like
$ bash script.sh -v VAR="bar" (sorry for the invention)
To set an environment variable for an invocation of a program, just add the assignment to the line before you call the script:
VAR=bar ./script.sh
Note that this won't override what is set in the script; so within the script, you will need to check if it's already set before setting it to a default value.
If you want this variable to be set for several invocations, then you can define it in your shell, and export it so that it will be in the environment of child processes:
$ export VAR=bar
$ ./script.sh

Problem in running a script

i have unix shell script which is need to be run like below
test_sh XYZ=KLMN
the content of the script is
#!/bin/ksh
echo $XYZ
for using the value of XYZ i have do set -k before i run the script.
is there a way where i can do this without doint set -k before running the script. or is there something that i can do in the script where i can use value of the parameter given while running the script in the below way
test_sh XYZ=KLMN
i am using ksh.
Any help is appreciated.
How about running this?
XYZ=KLMN ./test_sh //running from directory where test_sh is
If your script needs no other arguments, a quick and dirty way do to it is to put
eval "$#"
at the start of your script. This will evaluate the command line arguments as shell commands. If those commands are to assign a shell/environment variable, then that's what it will do.
It's quick-and-dirty since anything could be put on the command line, causing problems from a syntax error to a bad security hole (if the script is trusted).
I'm not sure if "$#" means the same in ksh as it does in bash - using just $* (without quotes) would work too, but is even dirtier.
It looks like you are trying to use the environment variable "INSTANCE" in your script.
For that, the environment variable must be set in advance of executing your script. Using the "set" command sets exportable environment variables. Incidentally, my version of ksh dates from 1993 and the "-k" option was obsolete back then.
To set an environment variable so that it is exported into spawned shells, simply use the "export" command like so:
export INSTANCE='whatever you want to put here'
If you want to use a positional parameter for your script -- that is have the "KLMN" value accessed within your script, and assuming it is the first parameter, then you do the following in your script:
#!/bin/ksh
echo $1
You can also assign the positional parameter to a local variable for later use in your script like so:
#!/bin/ksh
param_one=$1
echo $param_one
You can call this with:
test_sh KLMN
Note that the spacing in the assignment is important -- do not use spaces.
I am tring this option
#!/bin/ksh
echo $1
awk '{FS="=";print $2}' $1
and on the command line
test_sh INSTANCE=LSN_MUM
but awk is failing.is there any problem over here?
Probably #!/bin/ksh -k will work (untested).

Resources