带有 bash 脚本的复选框

发布于 2024-12-26 02:50:25 字数 2249 浏览 1 评论 0原文

我正在尝试做一个非常简单的 bash 脚本,它模拟复选框外观的行为!

我希望它显示一些选项,并根据向左或向右箭头键的按键将光标移动到下一个复选框。我已经设法使用 READ 和 Ansii 转义序列来检测箭头键来做到这一点,并使用 tput 来移动光标。

我的问题是我需要读取要按下的某个字母(例如 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 which 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

是的,我知道,这不是有史以来最完美的代码,但我正在努力学习。我们将非常感谢您的建议。

I am trying to do a very simple bash script which emulates the behavior of checkboxes in appearance!

I want it to show some options and move the cursor to the next checkbox according to the key press of the left or right arrow keys. I've already managed to do this using READ and Ansii escape sequences to detect the arrow keys and I use tput to move the cursor.

My problem with this it's that I need to read a certain letter (x for instance) to be pressed and then take another action. But how can I detect this key press and at the same time detect if an arrow key it's being pressed or not?

To detect the ansii codes I need to read 3 characters and with the X character (the key to "select") I need to read just one, how can I read 3 characters and at the same time read just one?

Also I've been trying to make something so the user can JUST press the left or right arrow keys or the x key but if he presses ANY other key, nothing should happen!

I've done this this far:

#!/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=

Yes I know, it's not the most perfect code ever but I'm trying to learn. Suggestions will be very appreciated.

\x1b\x5b\x44' #leftuierda right_arrow=

Yes I know, it's not the most perfect code ever but I'm trying to learn. Suggestions will be very appreciated.

\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 which 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

Yes I know, it's not the most perfect code ever but I'm trying to learn. Suggestions will be very appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从来不烧饼 2025-01-02 02:50:25

如果您只需要脚本中的复选框,则可以使用 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) 程序返回的选定条目。

如果您自己编程是为了好玩,请告诉我,我会将其转换为评论,希望其他人将来会发现该提示有用。

If you just want the checkboxes in a script, you can use the whiptail(1) or dialog(1) tools to create the checkboxes:

$ 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"$ 

The final "two" "four" is the selected entries returned from the whiptail(1) program.

If you're programming this for the fun of it yourself, let me know and I'll just convert this to a comment in the hopes that someone else will find the hint useful in the future.

巾帼英雄 2025-01-02 02:50:25

或者,您可以使用我的图书馆
https://sites.google.com/site/easybashgui

它会自动检测对话框whiptail 已安装。
(通常情况下,即使在裸系统中,也始终存在至少两个之一......)

因此您的脚本变为:

source easybashgui;菜单 1 2 3 ; clean_temp

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

Alternatively, you can use my library at
https://sites.google.com/site/easybashgui

It detects automatically if dialog or whiptail is installed.
(Normally, at least one of two is always present even in bare systems...)

So your scripts becomes:

source easybashgui; menu 1 2 3 ; clean_temp

Why reinventing the wheel?
;P

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文