使用(CLI GUI)对话框提示用户输入的 Bash shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4984288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:27:09  来源:igfitidea点击:

Bash shell script to prompt user for input with a (CLI GUI) dialog box

bashinputprompt

提问by Mike

Hi and first of all thanks to all those that give helpful answers/pointers!!

嗨,首先感谢所有提供有用答案/指点的人!!

I'm writing a Bash script that at times may need to ask the user for input. I'm looking for a tool to present a input box (think GUI pop up message box) at runlevel 3 (no X Windows) that provides a selection list and option for manual input. It should provide a radio button next to each option.

我正在编写一个有时可能需要询问用户输入的 Bash 脚本。我正在寻找一种工具来在运行级别 3(无 X Windows)上显示输入框(认为 GUI 弹出消息框),该工具提供选择列表和手动输入选项。它应该在每个选项旁边提供一个单选按钮。

For example if it were asking the user for a name it would present a list like this

例如,如果它要求用户输入一个名字,它会显示一个这样的列表



(*) Johnny

(*) 约翰尼

( ) Ben

( ) 本

( ) Hillary.

( ) 希拉里。

( ) <manual input>

( ) <manual input>



Where the <manual input>line would be a text box to allow the user to enter free text. The output should be to standard out and should be the name e.g. "Hillary".

<manual input>行将是一个文本框,允许用户输入自由文本。输出应该是标准输出并且应该是名称,例如“Hillary”。

In summary the requirements are:

概括起来要求是:

1) It should pop up and be a bright color to grab the user's attention. Flat text is not suitable

1)它应该弹出并且是一个明亮的颜色来吸引用户的注意力。纯文字不适合

2) Preferably allow selection using the cursor or a mouse

2) 最好允许使用光标或鼠标进行选择

3) Provide a radio button selection list

3) 提供单选按钮选择列表

4) Also allow for free text input

4) 还允许自由文本输入

5) Write the selection to standard out

5) 将选择写入标准输出

6) I don't want to wipe the status/log messages already on the screen

6) 我不想擦除屏幕上已有的状态/日志消息

Bash's read command is not "flashy" enough, it doesn't grab the user's attention.

Bash 的 read 命令不够“浮华”,它没有抓住用户的注意力。

The tool dialogwipes the text already on the screen as it re-draws the whole screen and does not allow for both a selection list and a free text input box in one screen.

工具对话框在重新绘制整个屏幕时会擦除屏幕上已有的文本,并且不允许在一个屏幕中同时包含选择列表和自由文本输入框。

Thanks again

再次感谢

MC

MC

回答by Vincent Stans

as sugested above dialog commands would work

如上所示,对话命令可以工作

Create a dialog with --keep-tite to keep your screen after input and using --checklist makes it posible to have radio select box. Place all the answers into a array and have a while loop echo each array item.

使用 --keep-tite 创建一个对话框以在输入后保留您的屏幕,并使用 --checklist 可以有单选框。将所有答案放入一个数组中,并让一个 while 循环回显每个数组项。

${#arrayName[@]} --> number of items in array or number of selection ${arrayName[@]} --> outputs all array items

${#arrayName[@]} --> 数组中的项数或选择数 ${arrayName[@]} --> 输出所有数组项

#!/bin/bash

result() {
i=0
echo there are ${#arrayName[@]} options selected
while (( i < ${#arrayName[@]} ))
do
  echo $i ${arrayName[$i]} 
  i=$(( $i + 1 ))
done
}

cmd=(dialog --separate-output --keep-tite --checklist "Select options:" 22 76 4)
options=(1 "Johnny" off
         2 "Ben" off
         3 "Hillary" off
         4 "User Input" off
)

choice=$("${cmd[@]}" "${options[@]}" 2>&1 > /dev/tty )

for answer in $choice
do
# make decsion 
case $answer in
    1) 
       arrayNum=${#arrayName[@]}  # arrayNum is the amount of items in arrayName 
       arrayName[$arrayNum]="Johnny" # if selected put Johnny in arrayName
       ;;
    2) 
       arrayNum=${#arrayName[@]}  # If johnny is selected ${#arrayName[@]} outputs 1 if not selected 0 
       arrayName[$arrayNum]="Ben" # If selected add to array
       ;;
    3) 
       arrayNum=${#arrayName[@]}
       arrayName[$arrayNum]="Hillary"
       ;;
    4) # If User Input is selected create an new dialog inputbox 

    user_input=$(\
    dialog --keep-tite --title "Enter Your Name" \
        --inputbox "Enter name:" 8 40 \
    3>&1 1>&2 2>&3 3>&- \
    )
       arrayNum=${#arrayName[@]}
       arrayName[$arrayNum]="$user_input"
       ;;
esac
done
result

回答by Noufal Ibrahim

I can think of 3 ways. YMMV

我能想到3种方法。青年会

  • Use Zenity(probably disqualified since you don't have X).
  • Use dialog- As for the thing on the screen disappearing, structure your script to use dialog completely and present even the informational text inside the widgets. It also has a --stdout option to direct it's output to the standard output so that it persists. Whiptail is a dialog replacement which I believe is lighter.
  • Use tputand manually move the cursor, draw colours etc. to make your dialogue box stand out. This is tedious and error prone.
  • 使用Zenity(可能被取消资格,因为您没有 X)。
  • 使用对话框- 至于屏幕上消失的东西,构建您的脚本以完全使用对话框,甚至在小部件内显示信息文本。它还有一个 --stdout 选项来将它的输出定向到标准输出,以便它持续存在。Whiptail 是一个对话替代品,我认为它更轻。
  • 使用tput并手动移动光标、绘制颜色等,使您的对话框脱颖而出。这是乏味且容易出错的。

I don't like bright colours and things especially for normal selections. I'd much prefer a subdued read. However, if you want to do this, I'd recommend the second approach. Your application should either use a widget toolkit or not use it. Don't try to make it do both. It's not worth the trouble.

我不喜欢明亮的颜色和东西,尤其是对于普通选择。我更喜欢柔和的read. 但是,如果您想这样做,我建议您使用第二种方法。您的应用程序要么使用小部件工具包,要么不使用它。不要试图让它两者兼而有之。这不值得麻烦。