Bash-安装带有用户输入的脚本-内部格式?

Bash-安装带有用户输入的脚本-内部格式?,bash,shell,Bash,Shell,显示: +--------------------------------------------------------------------+ | | | [*] User Input: | |

显示:

   +--------------------------------------------------------------------+ 
   |                                                                    | 
   |  [*] User Input:                                                   | 
   |                                                                    | 
   +--------------------------------------------------------------------+ 
代码:


如何将用户输入放入该框中?所以当他们键入响应时,它会在用户输入之后显示:然后将读取的输入拉入脚本的其余部分?我尝试过各种形式的将格式化代码放在读取输入的周围,但它总是出错。我怀疑有一种不同的方法可以做到这一点。

首先,我同意其他人的观点,即你不应该创造 交互式脚本。这些都很烦人,很难从其他系统中重复使用 脚本。确保您也从命令行接受相同的值 参数,即myscript-一些标志值1值2

第二,简单的

IFS= read -rep 'Please, input foo> ' foo
这样做的好处是用户可以使用 readline的历史狂欢

第三,如果你真的想要与“windows”交互的东西,使用 对话它是为这个设计的

第四,为了完成你的要求,你必须与 tput。我建议您阅读以下手册:

tput1:man 1 tput terminfo5:man 5 terminfo termcap 5:man 5 termcap 此示例脚本应为您提供一个想法:

#!/bin/bash

message=$'
   +--------------------------------------------------------------------+ 
   |                                                                    | 
   |  [*] User Input:                                                   | 
   |                                                                    | 
   +--------------------------------------------------------------------+
'

# trim the the leading and trailing unwanted whitespace
message=${message#*$'\n'}
message=${message%$'\n'*}

# clear the terminal
tput reset

# write the message to the terminal
printf '%s\n' "$message"

# save the cursor position, we're going to jump!
tput sc

# this is the string from the beginning of message up to the ':'
# character (not including ':')
up_to_colon="${message%:*}"

# the number of linefeeds in $up_to_colon will help us determine the
# number of rows (i.e. if the first line is row 0, the line where the
# ':' was will be the number of linefeeds).
linefeeds=${up_to_colon//[!$'\n']/}
row=${#linefeeds}

# we'll remove from the beginning to the last linefeed, leaving only
# the line where ':' was. The length of the remaining string will
# represent the position of the ':' (minus one, because strings are
# zero indexed). We'll shift it by two to end up two characters after
# the ':'. So instead of having:
# :[cursor]
# we will have
# : [cursor]
# (IMO this is nicer)
just_colon_line=${up_to_colon##*$'\n'}
column=$((${#just_colon_line} + 2))

# jump to the computed position.
tput cup "$row" "$column"

# ask the value from the user.
IFS= read -r value

# jump back to the end of $message
tput rc

# and the user typed:
printf "User's input was: %s\n" "$value"
以下是示例运行的外观:

   +--------------------------------------------------------------------+ 
   |                                                                    | 
   |  [*] User Input: My name is Neo                                    | 
   |                                                                    | 
   +--------------------------------------------------------------------+
User's input was: My name is Neo
dualbus@debian ~ % 
注意:tput重置会清除前面的内容,这使得跳转更容易 到特定的行、列,因为消息将是上的唯一内容 屏幕。如果你想保留上一段文字,你必须 查询当前的行、列,并进行一些计算,以确定在何处执行操作
跳我讨厌的东西

您需要具有非阻塞读取,即不需要输入键。查看read-n1读取一个按键并返回回车符以重新打印显示。好的,我知道你要做什么,这将在以后的脚本中证明是有用的。然而,我在寻找别的东西。用户输入只是示例文本,它实际上类似于选择要绑定到的IP-也许对话框会更好?也许可以查看Select以从菜单中进行选择?要求用户输入的安装脚本,如果在一些非标准wy中执行更多操作,这是一个非常糟糕的主意。想想自动化安装,也许是在计算机实验室的一个机器农场上……比如@vonbrand指出,出于对神圣事物的热爱,请不要使用自己的定制安装脚本。也就是说,对话才是你真正想要的。
   +--------------------------------------------------------------------+ 
   |                                                                    | 
   |  [*] User Input: My name is Neo                                    | 
   |                                                                    | 
   +--------------------------------------------------------------------+
User's input was: My name is Neo
dualbus@debian ~ %