调用bash脚本并从另一个bash脚本填充输入数据

时间:2022-11-30 01:46:56

I have a bash script, a.sh

我有一个bash脚本,a.sh

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

当我运行a.sh时,我需要填写几个读数。我们这样说吧

./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 ?

现在我在我的新脚本b.sh中调用a.sh.我可以让b.sh自动填写“测试”字符串吗?

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 ?

另外一个问题,a.sh在控制台上拥有大量的打印件,我可以通过在b.sh中执行某些操作而不更改a.sh来静音来自a.sh的打印件吗?

Thanks.

谢谢。

2 个解决方案

#1


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.

但是,您可能仍会看到提示,即使您没有看到满足这些提示的任何内容。那看起来很糟糕。此外,根据a.sh的作用,您可能需要它来从标准输入中读取更多信息 - 但您必须确保调用它的脚本提供正确的信息。

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.

但一般来说,你试图避免这种情况。提示输入的脚本不利于自动化。最好通过命令行参数提供输入。这使您的第二个脚本b.sh可以轻松地驱动a.sh.

#2


0  

a.sh

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

b.sh

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


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.

但是,您可能仍会看到提示,即使您没有看到满足这些提示的任何内容。那看起来很糟糕。此外,根据a.sh的作用,您可能需要它来从标准输入中读取更多信息 - 但您必须确保调用它的脚本提供正确的信息。

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.

但一般来说,你试图避免这种情况。提示输入的脚本不利于自动化。最好通过命令行参数提供输入。这使您的第二个脚本b.sh可以轻松地驱动a.sh.

#2


0  

a.sh

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

b.sh

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