Checkboxes with bash script

I am trying to make a very simple bash script that emulates the behavior of checkboxes in appearance! I want it to show some parameters and move the cursor to the next checkbox according to pressing the left or right arrow keys. I already managed to do this, using the READ and Ansii escape sequences to detect the arrow keys, and I use tput to move the cursor.

My problem is that I need to read a specific letter (x, for example) that needs to be clicked, and then take another action. But how can I detect this keystroke and at the same time determine if the arrow key is pressed or not?

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

I also tried to do something so that the user could just press the left or right arrow keys or the x key, but if he presses any other key, nothing should happen!

I made it 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=$'\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 

Yes, I know, this is not the most perfect code, but I'm trying to learn. Suggestions will be greatly appreciated.

+6
source share
2 answers

If you want to set 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" are the selected records returned by whiptail(1) .

If you program this for fun, let me know, and I will simply convert it into a comment in the hope that someone else will find a clue useful in the future.

+7
source

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

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

So your scripts become:

source easybashgui; menu 1 2 3 ; clean_temp

Why reinvent the wheel? ; R

+4
source

Source: https://habr.com/ru/post/905678/


All Articles