bash 中的方向键大小写

2024-02-27

如果按下上/左箭头键,是否可以在 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 https://github.com/jbondhus/last-seen/blob/master/last-seen.sh是完整的脚本。

注释为“等待用户输入”的读取命令是您按 Enter 键转到下一个用户的命令。基本上,这个脚本的作用是列出用户以及每个用户登录后经过的时间。我试图使用箭头键在显示的每个用户之间进行切换。我认为可以使用 case 语句来区分按键输入。重申我的观点,我不确定这是否可能。如果不是,有人能想出另一种方法来做到这一点吗?


您可以读取方向键以及其他键,无需任何异常命令;你只需要有条件地添加第二个read call:

escape_char=$(printf "\u1b")
read -rsn1 mode # get 1 character
if [[ $mode == $escape_char ]]; then
    read -rsn2 mode # read 2 more chars
fi
case $mode in
    'q') echo QUITTING ; exit ;;
    '[A') echo UP ;;
    '[B') echo DN ;;
    '[D') echo LEFT ;;
    '[C') echo RIGHT ;;
    *) >&2 echo 'ERR bad input'; return ;;
esac
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

bash 中的方向键大小写 的相关文章

  • 使用 tac 和 sed 反转文件

    我有一个用例 我需要搜索并替换文件中最后一次出现的字符串并将更改写回文件 下面的案例是该用例的简化版本 我正在尝试反转该文件 进行一些更改 再次将其反转并写入该文件 我为此尝试了以下代码片段 tac test sed s a b sed i
  • 何时用引号将 shell 变量括起来?

    我应该或不应该在 shell 脚本中用引号括住变量吗 例如 下列说法正确的是 xdg open URL eq 2 or xdg open URL eq 2 如果是这样 为什么 一般规则 如果它可以为空或包含空格 或实际上任何空格 或特殊字符
  • 如何通过ssh获取远程命令的退出代码

    我正在通过 ssh 从远程计算机运行脚本 ssh some cmd my script 现在 我想在本地计算机上存储 shell 脚本的退出状态 我该怎么做 假设没有任何问题ssh其本身 其退出状态是在远程主机上执行的最后一个命令的退出状态
  • 如何在 bash 脚本中使用并行编程/多线程?

    这是我的脚本 bin bash script to loop through directories to merge fastq files sourcedir path to source destdir path to dest fo
  • 向后台进程发送命令

    我有一个先前运行的进程 process1 sh 它正在后台运行 PID 为 1111 或其他任意数字 我怎样才能发送类似的东西command option1 option2PID 为 1111 的进程 I don t想要启动一个新的proc
  • 从 shell 查找不包含特定注释的 XML 文件

    我想搜索 awk grep sed 几个 XML 文件 pom xml 文件 跳过某些文件夹 而且 第一个条件是它们必须包含标签
  • 使用 sed 查找并替换包含非转义字符的变量

    我可以使用它来查找 fly 的所有实例 并将其替换为文件中的 insect sed i s fly insect g animals txt 如何找到 BASH 变量并将其替换为另一个 BASH 变量 例如 name echo fly ca
  • unix 下日期字段排序

    我有包含数十万条记录的文本文件 其中一个字段是日期字段 有没有办法根据日期字段对文件进行排序 09 APR 12 04 08 43 632279000 AM 19 MAR 12 03 53 38 189606000 PM 19 MAR 12
  • 使用 shell=True 将 PATH 设置为 bitbake 的“source”在 Python 中没有效果

    下面是shell脚本中的代码 source proj common tools repo etc profile d repo sh repo project init branch repo project sync source pok
  • grep 排除文件的数组参数

    我想从我的文件中排除一些文件grep命令 为此我使用参数 exclude excluded file ext 为了更容易阅读 我想使用包含排除文件的 bash 数组 EXCLUDED FILES excluded file ext 然后将
  • Slurm:提交到多个节点时出错(“slurmstepd:错误:execve():python:没有这样的文件或目录”)

    我有一个 bash 脚本submit sh用于向 Slurm 服务器提交训练作业 其工作原理如下 正在做 bash submit sh p1 8 config file 将提交一些对应的任务config file至 8 个 GPU 分区p1
  • 获取最新远程提交的 SHA1 [重复]

    这个问题在这里已经有答案了 可能的重复 git bash 如何检查是否有新的提交可用 https stackoverflow com questions 6006759 git bash how to check if theres a n
  • 批量删除文件名中包含 BASH 中特殊字符的子字符串

    我的目录中有一个文件列表 opencv calib3d so2410 so opencv contrib so2410 so opencv core so2410 so opencv features2d so2410 so opencv
  • 通过 bash 命令设置 gitlab-ci.yml 变量

    variables CUSTOM NODE VERSION cat nvmrc 我想要变量CUSTOM NODE VERSION通过内容填充 nvmrc文件 位于项目根目录中 如何在gitlab ci yml file 上面的例子不起作用
  • Bash:递归复制命名文件,保留文件夹结构

    我希望 cp R src prog js images icon jpg tmp package 将在目标目录中产生对称结构 tmp package src prog js images icon jpg 但相反 这两个文件都被复制到 tm
  • 如何制作 Bash 脚本来查找项目中未使用的图像?

    如何制作一个 Bash shell 脚本 它可以识别所有 jpg gif 和 png 文件 然后识别文件夹中任何文本文件中哪些文件未通过 url href 或 src 链接 这就是我开始的 但我最终得到了与我想要的相反的结果 我不想知道引用
  • 在 byobu 选项卡中启动命令的脚本

    我已经使用 screen 来启动服务器进程 以便稍后我可以在需要时附加并再次分离以继续执行其他操作 它工作得很好 但我最近发现了 byobu 我真的很喜欢它 我想使用相同类型的脚本来运行服务器 但我想将其附加到 byobu 选项卡 而不是屏
  • UNIX 统计时间格式

    是否可以格式化 stat 的时间输出 我在用 stat c n A z filename 在 bash 脚本中 但它的时间格式不是我想要的 是否可以在命令中更改此格式 或者我必须稍后手动执行此操作 示例输出如下 lib drwxr xr x
  • 静默检查 bash 脚本中是否存在 rpm

    我正在尝试使用 if 语句快速检查 rpm 是否安装在 bash 脚本中 但我想默默地做 目前 当我运行脚本并且 rpm 确实存在时 它将 rpm 的输出输出到我不想要的屏幕 if rpm qa grep glib then do some
  • 在 .gitconfig 中隐藏 GitHub 令牌

    我想将所有点文件存储在 GitHub 上 包括 gitconfig 这需要我将 GitHub 令牌隐藏在 gitconfig 中 为此 我有一个 gitconfig hidden token 文件 这是我打算编辑并放在隐藏令牌的 git 下

随机推荐