Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting variables between shell script

I have two independently running scripts. first one lets say script A calculates some values. And i want to echo these values from other script called B. These scripts will not call each other. I have used export keyword but didn't work. How can i do this?

like image 937
thetux4 Avatar asked Apr 15 '11 13:04

thetux4


People also ask

How do I pass arguments from one bash script to another?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

How do I export variables?

Usage: to export or Not to export We use environment variables (with export) when we want to export the variables and make them available to the subsequent commands or processes. Normally we use this to share the environment with a child process: Configure the environment of the child process or shell.

How do I export multiple variables?

Use named exports to export multiple variables in TypeScript, e.g. export const A = 'a' and export const B = 'b' . The exported variables can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.


3 Answers

If I understood the requirement then can't both scripts be simply executed in same sub shell, independently but without calling each other or without any need of an external file or pipe like this:

Let's say this is your script1.sh

#!/bin/bash
# script1.sh: do something and finally
export x=19

And here us your script2.sh

#!/bin/bash
# script2.sh: read value of $x here
echo "x="${x}

Just call them in same sub-shell like this

(. ./script1.sh && ./script2.sh)

OUTPUT:

x=19
like image 82
anubhava Avatar answered Sep 19 '22 11:09

anubhava


 mkfifo /tmp/channel

 process_a.sh > /tmp/channel&
 process_b.sh < /tmp/channel&

 wait

Of course you can also just read a single line when you want it.

In bash there are coprocs, which also might be what you want. Random example from this page

# let the output of the coprocess go to stdout
{ coproc mycoproc { awk '{print "foo" $0;fflush()}' ;} >&3 ;} 3>&1
echo bar >&${mycoproc[1]}
foobar

ksh has a similar feature, apparently

like image 30
sehe Avatar answered Sep 19 '22 11:09

sehe


Think of each script as a function: function A calculates some value and returns it. It does not know who will call it. Function B takes in some value and echo it. It does not care who produced that value. So, script A is:

#!/bin/sh
# a.sh: Calculate something and return the result
echo 19

and script B is:

#!/bin/sh
# b.sh: Consume the calculated result, which passed in as $1
echo The result is $1

Make them executable:

chmod +x [ab].sh

Now, we can glue them together on the command line:

$ b.sh $(a.sh)
The result is 19

Semantically, b.sh did not call a.sh. You called a.sh and passed its result to b.sh.

like image 45
Hai Vu Avatar answered Sep 18 '22 11:09

Hai Vu