arrow-detect.sh

来自「一本完整的描述Unix Shell 编程的工具书的所有范例」· Shell 代码 · 共 74 行

SH
74
字号
#!/bin/bash# arrow-detect.sh: Detects the arrow keys, and a few more.# Thank you, Sandro Magi, for showing me how.# --------------------------------------------# Character codes generated by the keypresses.arrowup='\[A'arrowdown='\[B'arrowrt='\[C'arrowleft='\[D'insert='\[2'delete='\[3'# --------------------------------------------SUCCESS=0OTHER=65echo -n "Press a key...  "# May need to also press ENTER if a key not listed above pressed.read -n3 key                      # Read 3 characters.echo -n "$key" | grep "$arrowup"  #Check if character code detected.if [ "$?" -eq $SUCCESS ]then  echo "Up-arrow key pressed."  exit $SUCCESSfiecho -n "$key" | grep "$arrowdown"if [ "$?" -eq $SUCCESS ]then  echo "Down-arrow key pressed."  exit $SUCCESSfiecho -n "$key" | grep "$arrowrt"if [ "$?" -eq $SUCCESS ]then  echo "Right-arrow key pressed."  exit $SUCCESSfiecho -n "$key" | grep "$arrowleft"if [ "$?" -eq $SUCCESS ]then  echo "Left-arrow key pressed."  exit $SUCCESSfiecho -n "$key" | grep "$insert"if [ "$?" -eq $SUCCESS ]then  echo "\"Insert\" key pressed."  exit $SUCCESSfiecho -n "$key" | grep "$delete"if [ "$?" -eq $SUCCESS ]then  echo "\"Delete\" key pressed."  exit $SUCCESSfiecho " Some other key pressed."exit $OTHER#  Exercises:#  ---------#  1) Simplify this script by rewriting the multiple "if" tests#+    as a 'case' construct.#  2) Add detection of the "Home," "End," "PgUp," and "PgDn" keys.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?