在bash中如何使用箭头键进行大小写转换

14

在bash脚本中,是否可以将箭头键用作触发器,当按下向上/向左箭头键时运行一组指令,按下向下/向右箭头键时运行另一组指令?我想要一种方法来在显示数据时使用箭头键快速切换用户,使用此脚本读取数据。

function main()  # The main function that controls the execution of all other functions
{
  mkdir -p ~/usertmp  # Make a new temporary user directory if it doesn't exist
  touch ~/last_seen_output.txt  # Create the output file if it doesn't exist
  cat /dev/null > ~/last_seen_output.txt  # Make sure that the output file is empty
  gather  # Call the "gather" function
  total=$((`wc -l ~/usertmp/user_list.txt|awk '{print $1}'`-1))  # Calculate the total amount of lines and subtract 1 from the result
  echo Current Time: `date +%s` > ~/last_seen_output.txt  # Print the current time to the output file for later reference
  echo "" > ~/last_seen_output.txt  # Print a blank line to the output file
    if [ $log -eq 1 ]
      then
        # If it is enabled, then delete the old backups to prevent errors
        while [ $line_number -le $total ]
          do

            line_number=$((line_number+1))  # Add 1 to the current line number
            calculate # Call the "calculate" function
            hms  # Call the "hms" function to convert the time in seconds to normal time
            log
        done
      else
        while [ $line_number -le $total ]
          do
            line_number=$((line_number+1))  # Add 1 to the current line number
            calculate # Call the "calculate" function
            hms  # Call the "hms" function to convert the time in seconds to normal time
            echo "Displaying, please hit enter to view the users one by one."
            read  # Wait for user input
            if [ "$log_while_displaying" ]
              then
                log
                display
              else
                display
            fi
        done
    fi
}
https://github.com/jbondhus/last-seen/blob/master/last-seen.sh 是完整的脚本。
read命令被注释为“等待用户输入”,它是您按Enter键以继续下一个用户的命令。基本上,此脚本会列出用户及其登录后经过的时间。我正试图使用箭头键在每个用户之间进行切换。我想这可能可以使用case语句来处理键盘输入。重申一下,我不确定这是否可行。如果不行,有人能想到其他方法吗?
11个回答

0

以上的答案都对我没用!我不得不从这个帖子中的许多答案以及通过谷歌搜索获得的其他信息中拼凑出一些东西。我花了大约一个小时来构思这个。

我正在运行Ubuntu 20.04 LTS,这对我有效(尽管可能不完美,因为我不得不“黑客”它):

waitkey() {
  local end=""
  local key=""

  echo
  echo "   Press ESC ... "

  while [ "$end" == "" ]; do
    read -rsn1 key
    case "$key" in
      $'\x1b')
        local k=""
        # I'm not sure why I have to do this if statement,
        # but without it, there are errors.  took forever
        # to figure out why 'read' would dump me outta the script
        if [ "$IFS" ]; then
          read -rsn1 -t 0.1 holder && k="$holder"
        else
          IFS=read -rsn1 -t 0.1 holder && k="$holder"
        fi 

        if [ "$k" == "[" ]; then
          read -rsn1 -t 0.1 holder && kk="$holder"

          ##############################
          # you put your arrow code here
          #
          # eg:
          #  case "$kk" in
          #    "A") echo "up arrow!" ;; # do something ...
          #  esac
          ##############################
        elif [ "$k" == "O" ]; then
          read -rsn1 -t 0.1 holder && kk="$holder"

          # I am honestly not knowing what this is for
        elif [ "$k" == "" ]; then
          end=1
        fi
    esac
  done
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接