0

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?

3 Answers 3

1

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

4 Comments

Actually I need the second case. I face a problem with the flag -r -r: command not found Indeed the manuals for read do not include the flag -r as far as I read. so could you please explain a little more
show the exact code that produced that error. What shell are you using?
I use bash shell on debian 2.30.2 and on ubuntu 14.04.1 However, here are my test scripts: master.sh: printf "%s\n" "1" "2" | ./func.sh func.sh: echo "enter 2 numbers:" first=read -r arg1 second=read -r arg2 echo "=================" echo $first echo $second and sorry because of the formatting of the code part in the comment..
1. the way to capture the output of a command is with $(), so output=$(command). 2. Just use the varname you want in the read command read -r first; read -r second
0

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.

Comments

0

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.

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.