从 shell 脚本中输入 [英] Input from within shell script

查看:99
本文介绍了从 shell 脚本中输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以调用需要用户输入的应用程序,例如运行需要用户输入Y"或N"的应用.
如何让 shell 脚本不要求用户输入,而是使用脚本中预定义变量的值?

I have a script that calls an application that requires user input, e.g. run app that requires user to type in 'Y' or 'N'.
How can I get the shell script not to ask the user for the input but rather use the value from a predefined variable in the script?

就我而言,有两个问题需要输入.

In my case there will be two questions that require input.

推荐答案

您可以在标准输入上输入任何您喜欢的文本,这与让用户自己输入一样.例如模拟输入Y"只需使用:

You can pipe in whatever text you'd like on stdin and it will be just the same as having the user type it themselves. For example to simulating typing "Y" just use:

echo "Y" | myapp

或使用 shell 变量:

or using a shell variable:

echo $ANSWER | myapp

还有一个名为yes"的 unix 命令,它为应用程序输出连续的y"流,这些应用程序提出了许多您只想肯定回答的问题.

There is also a unix command called "yes" that outputs a continuous stream of "y" for apps that ask lots of questions that you just want to answer in the affirmative.

这篇关于从 shell 脚本中输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆