Exporting variables between shell script

2019-04-08 00:44发布

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?

6条回答
聊天终结者
2楼-- · 2019-04-08 01:02
 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楼-- · 2019-04-08 01:02

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.

查看更多
SAY GOODBYE
4楼-- · 2019-04-08 01:05

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楼-- · 2019-04-08 01:11

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.
查看更多
手持菜刀,她持情操
6楼-- · 2019-04-08 01:14

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
查看更多
倾城 Initia
7楼-- · 2019-04-08 01:24

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.

查看更多
登录 后发表回答