在bash套管箭头键 [英] Casing arrow keys in bash

查看:196
本文介绍了在bash套管箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能区分箭头键在bash脚本来运行特定的命令集,如果向上/左箭头键是pressed,并有一定的一套,如果向下/右箭头键pressed ?我想有办法使用箭头键它显示的数据,同时,使用该脚本读取数据快速用户之间进行切换。

Is it possible to case arrow keys in a bash script to run a certain set of commands if the up/left arrow keys are pressed, and a certain set if the down/right arrow keys are pressed? I am trying to have a way to switch between users quickly by using the arrow keys while it is displaying the data, using this script to read the data from.

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-可见/ BLOB /主/ last-seen.sh 是完整的脚本。

读命令评价为等待用户输入是你敲回车进入下一个用户的命令。基本上,这是什么脚本它列表中的用户,并已经过去了,因为每个用户登录的时间。我试图通过使用箭头键显示每个用户之间进行切换。我想,这可能是可以使用一个case语句来区分按键输入。再次重申我的观点,我不知道这是可能的。如果不是,谁能想到另一种方式来做到这一点?

The read command commented as "wait for user input" is the command that you hit enter to go to the next user for. Basically, what this script does it list users and the time that has passed since each user logged in. I am trying to switch between each user being displayed by using the arrow keys. I figured that it might be possible to use a case statement to case the key input. To reiterate my point, I'm not sure if this is possible. If it isn't, can anyone think of another way to do this?

推荐答案

您可以使用读-n 1 来读取一个字符,然后用案例来选择一个操作语句来利用基于关键。

You can use read -n 1 to read one character then use a case statement to choose an action to take based on the key.

在问题比一个字符,序列(和它的长度)从终端到终端的变化方向键输出的更多。

On problem is that arrow keys output more than one character and the sequence (and its length) varies from terminal to terminal.

例如,我使用的终端上,右箭头输出 ^ [[C 。你可以看到pressing何种顺序终端输出<大骨节病>控制 - <大骨节病> V <大骨节病>向右箭头。这同样适用于其他光标控制键,如<大骨节病>上页和<大骨节病>结束

For example, on the terminal I'm using, the right arrow outputs ^[[C. You can see what sequence your terminal outputs by pressing Ctrl-V Right Arrow. The same is true for other cursor-control keys such as Page Up and End.

我会建议,相反,使用单字符键,如&LT; &GT; 。处理这些问题在你的脚本就会简单得多。

I would recommend, instead, to use single-character keys like < and >. Handling them in your script will be much simpler.

read -n 1 key

case "$key" in
    '<') go_left;;
    '>') go_right;;
esac

这篇关于在bash套管箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆