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?
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.
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.
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.
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With