thundium
thundium

Reputation: 1063

call bash script and fill input data from another bash script

I have a bash script, a.sh

And when I run a.sh, I need to fill several read. Let's say it like this

./a.sh
Please input a comment for script usage
test (I need to type this line mannually when running the script a.sh, and type "enter" to continue)

Now I call a.sh in my new script b.sh. Can I let b.sh to fill in the "test" string automaticlly ?

And one other question, a.sh owns lots of prints to the console, can I mute the prints from a.sh by doing something in my b.sh without changing a.sh ?

Thanks.

Upvotes: 0

Views: 2769

Answers (2)

micromoses
micromoses

Reputation: 7507

a.sh

#!/bin/bash
read myvar
echo "you typed ${myvar}"

b.sh

#!/bin/bash
echo "hello world"

You can do this in 2 methods:

$ ./b.sh | ./a.sh
you typed hello world
$ ./a.sh <<< `./b.sh`
you typed hello world

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 755026

Within broad limits, you can have one script supply the standard input to another script.

However, you'd probably still see the prompts, even though you'd not see anything that satisfies those prompts. That would look bad. Also, depending on what a.sh does, you might need it to read more information from standard input — but you'd have to ensure the script calling it supplies the right information.

Generally, though, you try to avoid this. Scripts that prompt for input are bad for automation. It is better to supply the inputs via command line arguments. That makes it easy for your second script, b.sh, to drive a.sh.

Upvotes: 1

Related Questions