call bash script and fill input data from another

2019-09-05 22:21发布

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.

标签: linux bash
2条回答
狗以群分
2楼-- · 2019-09-05 22:35

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
查看更多
一纸荒年 Trace。
3楼-- · 2019-09-05 22:51

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.

查看更多
登录 后发表回答