简体   繁体   English

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

[英]call bash script and fill input data from another bash script

I have a bash script, a.sh 我有一个bash脚本a.sh

And when I run a.sh, I need to fill several read. 而当我运行a.sh时,我需要填写几读。 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. 现在,我在新脚本b.sh中调用a.sh。 Can I let b.sh to fill in the "test" string automaticlly ? 我可以让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. 谢谢。

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

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: 您可以通过2种方法执行此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM