使用 Bash 脚本将输入提供给命令行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23174849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:16:06  来源:igfitidea点击:

Using Bash Script to feed input to command line

linuxbash

提问by Johsh Hanks

So I am trying to make a bash script that calls this one command and then feeds it the input. It calls this one command and the command needs 3-4 inputs after. I type the command and it waits for me to enter first name, one I enter first name it waits for me to enter last name, and so on. How can I use bash script to pass these arguments to the command one at a time?

所以我试图制作一个 bash 脚本来调用这个命令,然后将输入提供给它。它调用这一命令,该命令之后需要 3-4 个输入。我输入命令,它等待我输入名字,一个我输入名字它等待我输入姓氏,依此类推。如何使用 bash 脚本一次将这些参数传递给命令?

回答by Barmar

A couple of ways.

几种方式。

Group all the echo commands and pipe them to the command:

将所有 echo 命令分组并将它们通过管道传输到命令:

{ echo $firstname; echo $lastname ; } | somecommand

or use a heredoc:

或使用heredoc:

somecommand <<EOF
$firstname
$lastname
EOF