Exporting variables between shell script

别说谁变了你拦得住时间么 提交于 2020-01-01 09:43:11

问题


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?


回答1:


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



回答2:


 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




回答3:


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.




回答4:


Can't you read a third file, let's say settings.sh with the common exported variables?

# common.sh
export MY_PATH=/home/foo/bar/
export VERSION=42.2a

and in both A and B source common.sh to load those values.

Note that the export may not be required in that case.




回答5:


You may echo values to files, then the other script may read them. If you want to use it as a parameter for something, use reverse aposthrophe:

echo `cat storedvalue`

Be careful, if the two scripts are running at the same time, concurrency problems may occur, which can cause rarely appearing, mystic-looking errors.




回答6:


Actually all your need is source you can skip the export prefix.

My use-case was an environment specific settings file, example:

Within main_script.sh

THIS_DIR=`dirname $0`
source $THIS_DIR/config_vars.sh
# other commands

Within config_vars.sh

LOCAL_ROOT="$HOME/folder/folder"
REMOTE_USERNAME='someuser'
BACKUP_FOLDER="$LOCAL_ROOT/folder"
# etc.


来源:https://stackoverflow.com/questions/5677446/exporting-variables-between-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!