带有bash脚本的复选框

前端之家收集整理的这篇文章主要介绍了带有bash脚本的复选框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图做一个非常简单的bash脚本,它模仿外观中复选框的行为!
我希望它显示一些选项,并根据按下左或右箭头键将光标移动到下一个复选框.我已经设法使用READ和Ansii转义序列来检测箭头键,并使用tput来移动光标.

我的问题在于我需要读取某个字母(例如x)才能按下然后再采取其他操作.但是,如何检测此按键,同时检测是否按下了箭头键?

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

此外,我一直在尝试制作一些东西,以便用户只需按左或右箭头键或x键,但如果他按下任何其他键,则不会发生任何事情!

我到目前为止做到了这一点:

  1. #!/bin/bash
  2. ## Here I just print in the screen the "form" with the "checkBoxes"
  3. function screen_info(){
  4. clear
  5. cat <<EOF
  6.  
  7. /\_/\_/\_/\_/\_/\_/\_/\_/\_/\_
  8. ||
  9. || 1[ ] 2[ ] 3[ ]
  10. ||
  11. #############################
  12.  
  13.  
  14.  
  15. EOF
  16. }
  17.  
  18. ## This function detects the arrow keys and moves the cursor
  19. function arrows(){
  20.  
  21. ## I use ANSII escape sequences to detect the arrow keys
  22. left_arrow=$'\x1b\x5b\x44' #leftuierda
  23. right_arrow=$'\x1b\x5b\x43' #rightecha
  24. just_x_key=""
  25.  
  26. ## With tput I move the cursor accordingly
  27. cursor_x=14
  28. cursor_y=3
  29. tput cup $cursor_y $cursor_x
  30.  
  31. while [ -z "$just_x_key" -o "$just_x_key" != "$just_x_key" ]; do
  32. read -s -n3 key
  33. while [ `expr length "$key"` -ne 3 ]; do
  34. key=""
  35. read -s -n3 key
  36. break
  37. done
  38. case "$key" in
  39. $left_arrow)
  40. if [ $cursor_x -gt 14 ]; then
  41. cursor_x=`expr $cursor_x - 8`
  42. fi
  43. tput cup $cursor_y $cursor_x
  44. #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 ???? =/
  45. #read -s just_x_key
  46. #if [ $just_x_key == x ]; then
  47. # echo X
  48. # tput cup 7 15
  49. # echo "YOU PRESSED THE RIGHT KEY!!! =D"
  50. #fi
  51. ;;
  52. $right_arrow)
  53. if [ $cursor_x -lt 28 ]; then
  54. cursor_x=`expr $cursor_x + 8`
  55. fi
  56. tput cup $cursor_y $cursor_x
  57. #read -s just_x_key
  58. #if [ $just_x_key == x ]; then
  59. # echo X
  60. # tput cup 7 15
  61. # echo "YOU PRESSED THE RIGHT KEY!!! =D"
  62. #fi
  63. ;;
  64. esac
  65. done
  66.  
  67. exit $?
  68. }
  69.  
  70.  
  71. #EXECUTION
  72. #=========
  73.  
  74. ## I just call the functions!
  75. screen_info
  76. arrows

是的,我知道,这不是最完美的代码,但我正在努力学习.建议将非常感谢.

如果您只想要脚本中的复选框,可以使用whiptail(1)或dialog(1)工具创建复选框:
  1. $whiptail --checklist "Please pick one" 10 60 5 one one off two two off\
  2. three three off four four off five five off
  3.  
  4.  
  5. ┌──────────────────────────────────────────────────────────┐
  6. [ ] one one
  7. [*] two two
  8. [ ] three three
  9. [*] four four
  10. [ ] five five
  11. <Ok> <Cancel>
  12. └──────────────────────────────────────────────────────────┘
  13.  
  14. "two" "four"$

最后的“两个”“四”是从whiptail(1)程序返回的选定条目.

如果你是为了自己的乐趣编程,请告诉我,我只是将其转换为评论,希望其他人能够在将来找到有用的提示.

猜你在找的Bash相关文章