在 bash 脚本中建议用户输入的答案
这是一个例子:
#!/bin/bash
echo -e "Enter IP address: \c"
read
echo $REPLY
但我想让用户更容易回答。我想为用户提供一个答案。它应该看起来像这样:
输入您的IP:192.168.0.4
用户可以直接按回车键同意192.168.0.4,也可以删除一些字符(例如用一个退格键删除“4”并输入3)。
如何进行这样的输入?在bash中可以吗?
Here is an example:
#!/bin/bash
echo -e "Enter IP address: \c"
read
echo $REPLY
But I want to make it easier for the user to answer. I'd like to offer an answer to the user. It should look something like this:
Enter your IP: 192.168.0.4
And the user can just press Enter and agree with 192.168.0.4, or can delete some characters (for example delete "4" with one backspace and type 3 instead).
How to make such an input? It is possible in bash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
bash 的读取具有 readline 支持(编辑:Jonathan Leffler 建议将提示也放入读取中)
bash's read has readline support (Edit: Jonathan Leffler suggests to put the prompt into read as well)
我执行此操作的方法是在括号中的提示中建议默认值,然后使用默认值参数扩展将
IP
设置为192.168.0.4(如果他们只是按回车键) ,否则它将具有他们输入的值。输出
The way I would do this is to suggest the default in the prompt in brackets and then use the default value parameter expansion to set
IP
to 192.168.0.4 if they just pressed enter, otherwise it will have the value they entered.Output
完成您想要的大部分操作的经典方法是:
这不提供编辑选项。
The classic way to do most of what you want is:
This doesn't give the editing option.
编辑并不实用,但通常会执行以下操作:
Editing isn't practical but it's common to do something like:
如果有循环,您可以建议最后一个值:
You can suggest last value if you have a loop: