如何使用空格键创建一个要求用户输入的shell脚本?

时间:2017-11-06 16:32:52

标签: shell installation radio-button

我想创建一个shell脚本,要求用户使用空格键选择单选按钮。我搜索了很多但是找不到任何东西。

以下是我所说的一个例子:

enter image description here

1 个答案:

答案 0 :(得分:0)

请参阅此主题以了解如何记录击键:bash scripting - read single keystroke including special keys enter and space

但是,您还需要使用need来回显提供给相同行的选项,而不是编写新行:How to show and update echo on same line

所以在这种情况下,你的脚本看起来像:

#!/bin/bash

SELECT=""
while [[ "$SELECT" != $'\x0a' && "$SELECT" != $'\x20' ]]; do
    echo "Press <Space> to move selection"
    echo "Press <Enter> to confirm selection"
    read -d'' -s -n1
    echo "Debug/$SELECT/${#SELECT}"
    [[ "$SELECT" == $'\x0a' ]] && echo "enter" # do your install stuff
    [[ "$SELECT" == $'\x20' ]] && echo "space" && echo -ne "$options" # reprint options  
done

编辑:实际上printf在这种情况下可能会更好。

相关问题