tj1*_*111 95 linux bash automation prompt command-prompt
是否可以让bash脚本自动处理通常以默认操作呈现给用户的提示?目前我正在使用bash脚本来调用内部工具,该工具将向用户显示提示(提示Y/N)以完成操作,但是我写的脚本需要完全"不干涉",所以我需要一种方法发送Y|N到提示,以允许程序继续执行.这可能吗?
Loï*_*ier 148
一个简单的
echo "Y Y N N Y N Y Y N" | ./your_script
Run Code Online (Sandbox Code Playgroud)
这允许您将任何"Y"或"N"序列传递给脚本.
小智 12
我发现发送输入的最佳方法是使用cat和文本文件来传递你需要的任何输入.
cat "input.txt" | ./Script.sh
Run Code Online (Sandbox Code Playgroud)
dou*_*rdo 12
如果你只有Y发送:
$> yes Y |./your_script
Run Code Online (Sandbox Code Playgroud)
如果你只有N发送:
$> yes N |./yout_script
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我需要回答一些没有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
Run Code Online (Sandbox Code Playgroud)
我喜欢这个策略的是你可以评论你的答案是什么,并使用EOF一个空行就是那个(默认答案).顺便说一下,这个导出工具有自己的JSON对应方式来回答这些问题,但是在我这样做之后我想出了这个=).
运行脚本只需在您想要的目录中并'sh autocomplete.sh'在终端中运行.
简而言之,通过将<< EOL&EOF 与Return Lines结合使用,您可以根据需要回答每个提示问题. 每个新行都是一个新答案.
我的例子只是展示了如何使用`字符进行注释,以便你记住每个步骤是什么.
注意这种方法的另一个优点是你可以用更多的答案来回答Y或N ...实际上你可以用空白来回答!
希望这有助于某人.
有一个特殊的内置实用程序 - '是'。
要以相同的答案回答所有问题,您可以运行
yes [answer] |./your_script
Run Code Online (Sandbox Code Playgroud)
或者你可以把它放在你的脚本中,每个问题都有特定的答案
| 归档时间: |
|
| 查看次数: |
123675 次 |
| 最近记录: |