6

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?

3
  • 1
    Will not . script do the trick? (see linuxquestions.org/questions/linux-newbie-8/…) Commented Oct 16, 2012 at 11:57
  • @mlvljr You should make this an answer, because it was what I needed. Thanks. Commented Jul 20, 2014 at 1:54
  • @AdamGriffiths Well, I'm trying to avoid the reputation point race here ;) Commented Jul 20, 2014 at 17:33

6 Answers 6

7

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
Sign up to request clarification or add additional context in comments.

Comments

4
 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

2 Comments

communication between separate processes is easiest done with files, and a named pipe provides a handy file-like interface: A "FIFO" is a special file type that permits independent processes to communicate.
coproc is interesting. i'll check it out.
1

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.

Comments

0

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.

1 Comment

I dont need third file actually.
0

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.

1 Comment

So how can i pass return value of script A's to the script B?
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.