Taktech
Taktech

Reputation: 485

Passing several values between two shell scripts

I have two shell scripts, i.e. (master, function); the master calls the function-script and tries to pass values to it.

Please note that function-script is an interactive script; i.e. it waits for user's answers to perform according to the answer.

So to pass one value I can write the following:

echo "string" | ./function-script

The problem is that I have to pass several values. Any advice?

Upvotes: 0

Views: 407

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753805

Use command line arguments.

./function-script "string" "another string"

If you pre-empt standard input by piping data into the function script, you make interactive operation of the function script hard.

You could instead export the variables as environment variables, but just as global variables in regular programming are not a good idea because their use is hidden, so too with environment variables.

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246807

Can the "function-script" operate on positional parameters? If so, you'd call it like:

./function-script arg1 "argument 2" arg3

And then "function-script" would use "$1", "$2" and "$3" as required.

If "function-script" only takes input on stdin, do something like this:

printf "%s\n" arg1 "argument 2" arg3 | ./function-script

And "function-script" would do:

IFS= read -r arg1
IFS= read -r arg2
IFS= read -r arg3

Upvotes: 1

anubhava
anubhava

Reputation: 785156

Simple solution:

  • Don't try to pass multiple variable.
  • Just export all the variable within master script using export a=1 syntax.
  • Then call child script from master like a regular script
  • All the variable will be available in child script.

Upvotes: 0

Related Questions