Note that there are some explanatory texts on larger screens.

plurals
  1. POCheckboxes with bash script
    primarykey
    data
    text
    <p>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.</p> <p>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??</p> <p>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?</p> <p>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!</p> <p>I've done this this far:</p> <pre><code>#!/bin/bash ## Here I just print in the screen the "form" with the "checkboxes" function screen_info(){ clear cat &lt;&lt;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 </code></pre> <p>Yes I know, it's not the most perfect code ever but I'm trying to learn. Suggestions will be very appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload