Bash - 使用用户输入安装脚本 - 内部格式化?

时间:2014-04-11 20:11:07

标签: bash shell

显示:

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

代码:

echo "   +--------------------------------------------------------------------+ "
echo "   |                                                                    | "
echo "   |  [*] User Input:                                                   | "
echo "   |                                                                    | "
echo "   +--------------------------------------------------------------------+ "

如何将用户输入放入该框?因此,当他们输入他们的回复时,它会显示在"用户输入后:"然后拉出"读取输入"进入脚本的其余部分?我已尝试过各种形式的格式化代码围绕"读取输入"但它只是一直在破碎。我怀疑有不同的做法。

1 个答案:

答案 0 :(得分:1)

首先,我同意其他人的观点,即你不应该创造 交互式脚本。这些都很烦人(难以从其他人那里重用) 脚本)。确保您也从命令行接受相同的值 参数(即myscript --some-flag value1 value2)。

其次,简单的错误是什么:

IFS= read -rep 'Please, input foo> ' foo

这样可以为用户提供使用的可能性 readline的历史(bash)。

第三,如果您真的想要使用' windows'进行互动,请使用 对话。这是为了这个!

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

  • tput(1):'' man 1 tput''
  • terminfo(5):'' 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重置会清除之前的内容,这样可以更容易地跳转 到特定的(行,列),因为消息将是唯一的东西 屏幕。如果你想保留以前的文字,你必须这样做 查询当前(行,列)并进行一些数学运算以找出位置 跳。 IMO无聊的东西!