2012-01-11 99 views
6

我想做一个非常简单的bash脚本,模拟外观复选框的行为! 我希望它显示一些选项,并根据按下左或右箭头键将光标移动到下一个复选框。我已经设法使用READ和Ansii转义序列来检测箭头键,并使用tput来移动光标。复选框与bash脚本

我的问题是这是我需要阅读某个字母(例如x),然后采取另一个行动。但是,我怎么能检测到这个按键,同时检测它是否被按下的箭头键?

为了检测ansii代码,我需要读取3个字符和X字符(“选择”键),我只需要读一个字符,如何读取3个字符并同时读取一个字符?

此外,我一直在尝试做一些东西,使用户只需按下左或右箭头键或x键,但如果他按任何其他键,什么都不应该发生!曾经

#!/bin/bash 
## Here I just print in the screen the "form" with the "checkboxes" 
function screen_info(){ 
clear 
cat <<EOF 

    /\_/\_/\_/\_/\_/\_/\_/\_/\_/\_ 
    || 
    ||  1[ ] 2[ ] 3[ ] 
    || 
    ############################# 



EOF 
} 

## This function detects the arrow keys and moves the cursor 
function arrows(){ 

## I use ANSII escape sequences to detect the arrow keys 
left_arrow=$'\x1b\x5b\x44' #leftuierda 
right_arrow=$'\x1b\x5b\x43' #rightecha 
just_x_key="" 

## With tput I move the cursor accordingly 
cursor_x=14 
cursor_y=3 
tput cup $cursor_y $cursor_x 

while [ -z "$just_x_key" -o "$just_x_key" != "$just_x_key" ]; do 
    read -s -n3 key 
    while [ `expr length "$key"` -ne 3 ]; do 
     key="" 
     read -s -n3 key 
     break 
    done 
    case "$key" in 
    $left_arrow) 
     if [ $cursor_x -gt 14 ]; then 
      cursor_x=`expr $cursor_x - 8` 
     fi 
     tput cup $cursor_y $cursor_x 
     #This is supposed to be a simple condition detecting the x key pressed wich I want to trigger something... But how to read a single character to this condition and 3 characters at the same time if the user presses the arrow key ???? =/ 
     #read -s just_x_key 
     #if [ $just_x_key == x ]; then 
     # echo X 
     # tput cup 7 15 
     # echo "YOU PRESSED THE RIGHT KEY!!! =D" 
     #fi 
     ;; 
    $right_arrow) 
     if [ $cursor_x -lt 28 ]; then 
      cursor_x=`expr $cursor_x + 8` 
     fi 
     tput cup $cursor_y $cursor_x 
     #read -s just_x_key 
     #if [ $just_x_key == x ]; then 
     # echo X 
     # tput cup 7 15 
     # echo "YOU PRESSED THE RIGHT KEY!!! =D" 
     #fi 
     ;; 
    esac 
done 

exit $? 
} 


#EXECUTION 
#========= 

## I just call the functions! 
screen_info 
arrows 

是的,我知道,这不是最完美的代码,但我想了解:

我已经这样做了这么远。建议将非常感激。

+0

你看过bash'select'语句了吗?这不完全是复选框,但它是一个编号菜单。 – SiegeX 2012-01-11 04:43:54

+0

@SiegeX是的,我知道这个命令并尝试过,但是我没有找到一种方法来迫使它只接受键盘上的某些键。我希望我的小脚本能够保护用户安全= P我的意思是用户无法输入任何内容的方式!使用select即使它很有用,如果选项是1,2,3,用户可以键入idfsbhisdolaso​​kjcmnh,我希望它们被迫只键入需要的内容,并将键盘的其余部分“锁定”。感谢您的回复;) – Metafaniel 2012-01-11 15:28:53

回答

7

如果你只是想在脚本中的复选框,可以使用whiptail(1)dialog(1)工具来创建复选框:

$ whiptail --checklist "Please pick one" 10 60 5 one one off two two off\ 
    three three off four four off five five off 


       ┌──────────────────────────────────────────────────────────┐ 
       │ [ ] one one          │ 
       │ [*] two two          │ 
       │ [ ] three three          │ 
       │ [*] four four          │ 
       │ [ ] five five          │ 
       │               │ 
       │    <Ok>     <Cancel>    │ 
       │               │ 
       └──────────────────────────────────────────────────────────┘ 

"two" "four"$ 

最后"two" "four"是所选条目从whiptail(1)程序返回。

如果你自己编写它的乐趣,让我知道,我只是将它转换为一个评论,希望别人能够在未来找到有用的提示。

+0

我阅读了关于对话框的内容,但是我想做一些事情,因为我不想安装任何其他的东西,默认情况下它没有安装(用于Lubuntu 10。10),但我不知道whiptail,它安装在发行版中,我需要运行脚本= D我一定会试试!谢谢。但是,为了学习紫袍,我花了几个小时在我的小脚本中,知道如何克服读取1个字符并同时读取3个字符会很有趣;)感谢您的回复,相当有用;) – Metafaniel 2012-01-11 15:35:41

4

或者,您也可以在 https://sites.google.com/site/easybashgui

使用我的图书馆它会检测是否自动对话框鞭尾安装。 (通常情况下,两个至少一个是始终存在的,即使在准系统...)

所以,你的脚本变为:

source easybashgui; menu 1 2 3 ; clean_temp

为什么重新发明轮子? ; P