是否可以让 bash 脚本自动处理通常使用默认操作呈现给用户的提示?目前我正在使用 bash 脚本来调用内部工具,该工具将向用户显示提示(提示 Y/N)以完成操作,但是我正在编写的脚本需要完全“放手”,所以我需要一种发送Y|N
到提示符以允许程序继续执行的方法。这可能吗?
问问题
189336 次
6 回答
208
一个简单的
echo "Y Y N N Y N Y Y N" | ./your_script
这允许您将任何“Y”或“N”序列传递给您的脚本。
于 2010-09-27T14:12:44.867 回答
26
如果您只有 Y 要发送:
$> yes Y |./your_script
如果您只有 N 要发送:
$> yes N |./your_script
于 2016-11-16T10:23:58.873 回答
22
我发现发送输入的最佳方式是使用 cat 和一个文本文件来传递您需要的任何输入。
cat "input.txt" | ./Script.sh
于 2015-07-22T14:23:58.177 回答
13
在我的情况下,我需要回答一些没有 Y 或 N 但有文本或空白的问题。我发现在我的情况下最好的方法是创建一个 shellscript 文件。就我而言,我称之为 autocomplete.sh
我需要为学说模式导出器回答一些问题,所以我的文件看起来像这样。
--这只是一个例子--
php vendor/bin/mysql-workbench-schema-export mysqlworkbenchfile.mwb ./doctrine << EOF
`#Export to Doctrine Annotation Format` 1
`#Would you like to change the setup configuration before exporting` y
`#Log to console` y
`#Log file` testing.log
`#Filename [%entity%.%extension%]`
`#Indentation [4]`
`#Use tabs [no]`
`#Eol delimeter (win, unix) [win]`
`#Backup existing file [yes]`
`#Add generator info as comment [yes]`
`#Skip plural name checking [no]`
`#Use logged storage [no]`
`#Sort tables and views [yes]`
`#Export only table categorized []`
`#Enhance many to many detection [yes]`
`#Skip many to many tables [yes]`
`#Bundle namespace []`
`#Entity namespace []`
`#Repository namespace []`
`#Use automatic repository [yes]`
`#Skip column with relation [no]`
`#Related var name format [%name%%related%]`
`#Nullable attribute (auto, always) [auto]`
`#Generated value strategy (auto, identity, sequence, table, none) [auto]`
`#Default cascade (persist, remove, detach, merge, all, refresh, ) [no]`
`#Use annotation prefix [ORM\]`
`#Skip getter and setter [no]`
`#Generate entity serialization [yes]`
`#Generate extendable entity [no]` y
`#Quote identifier strategy (auto, always, none) [auto]`
`#Extends class []`
`#Property typehint [no]`
EOF
我喜欢这个策略的一点是你可以评论你的答案是什么,并且使用 EOF 一个空行就是这样(默认答案)。事实证明,这个导出器工具有自己的 JSON 对应物来回答这些问题,但在我这样做之后我想通了 =)。
运行脚本只需在您想要的目录中并'sh autocomplete.sh'
在终端中运行。
简而言之,通过将<< EOL & EOF 与 Return Lines 结合使用,您可以根据需要回答提示的每个问题。 每一条新线都是一个新的答案。
我的示例只是展示了如何使用 ` 字符通过注释来完成此操作,以便您记住每个步骤是什么。
请注意,此方法的另一个优点是您可以回答更多而不是 Y 或 N ...实际上您可以回答空白!
希望这可以帮助某人。
于 2017-02-13T01:29:48.613 回答
7
于 2018-11-25T07:02:09.360 回答