0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

If you're going to use a herestring, it's more reliable to quote the value -- otherwise, you'll hit bugs in some (but not all) bash versions. Make it <<< "$(./b.sh)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.