AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 640819
Accepted
kos
kos
Asked: 2015-06-26 04:27:41 +0800 CST2015-06-26 04:27:41 +0800 CST 2015-06-26 04:27:41 +0800 CST

处理一定长度的 bash 中断命令

  • 772

假设您想在以下位置运行此命令bash:

echo -n "Command of a specific length will break at the second run attempt."

这是 24x80 TTY 的输出:

ubuntu@ubuntu:~$ echo -n "Command of a specific length will break at the second 
run attempt."
Command of a specific lenght will break at the second run attempt.ubuntu@ubuntu:
~$ 

假设您想再次运行相同的命令。

一旦你点击↑:

ubuntu@ubuntu:~$ echo -n "Command of a specific lenght will break at the second
run attempt."
Command of a specific lenght will break at the second run attempt.ubuntu@ubuntu:
run attempt."ommand of a specific lenght will break at the second r

这种情况发生在特定大小的命令中,这些命令最后不打印换行符。

这非常烦人。到目前为止,我还没有找到解决这个问题的方法,我认为遇到这个问题的每个人都会非常感谢能解决这个问题的人。

command-line
  • 3 3 个回答
  • 434 Views

3 个回答

  • Voted
  1. muru
    2015-06-26T04:42:30+08:002015-06-26T04:42:30+08:00

    使用更智能的外壳,例如zsh:

    在此处输入图像描述

    请注意它是如何添加一个%以指示缺少换行符并在不同的行上打印提示的。

    • 4
  2. terdon
    2015-06-26T04:33:12+08:002015-06-26T04:33:12+08:00

    就打CtrlL。这将重绘您的终端并使所有内容按应有的方式显示:

    在此处输入图像描述

    • 3
  3. Best Answer
    Aaron McDaid
    2015-06-27T01:32:48+08:002015-06-27T01:32:48+08:00

    这也可以完成bash。诀窍是使用一个自定义PROMPT_COMMAND查询终端的光标位置(根据这个问题)。

    这个解决方案可能可以扩展到其他 shell,但我只熟悉bash. (有关解决方案,请参阅@muru 的回答zsh)。也许已经有一个选项bash可以自动执行此操作。

    把这个放在你的.bashrc:

    function new.line.if.not.on.left {
        local foo
        local garbage
        local column
        echo -n -e "\033[6n"     # as the terminal for the position
        read -s -d \[ garbage    # ignore the first part of the response
        read -s -d R foo         # store the position in foo
        column="$(echo "$foo" | cut -d';' -f2)"    # skip over the row number
        test "$column" "!=" 1 && { tput smso; echo "%"; tput rmso; }
    }
    
    PROMPT_COMMAND="new.line.if.not.on.left; $PROMPT_COMMAND"
    

    最后一行预先调用new.line.if.not.on.left您的PROMPT_COMMAND(因为您可能已经PROMPT_COMMAND定义了)。

    bash 函数的new.line.if.not.on.left工作原理如下:

    • echo -n -e "\033[6n"是一个魔术,它向终端询问光标当前位置的行和列。终端通过发送带有答案的假键盘输入来“响应”。
    • read -s -d \[ garbage. 响应的第一部分是一些乱码,某种转义码。通过将其存储在garbage.
    • read -s -d R foo. 将假键盘响应存储在 bash 变量foo中。-s需要停止再次将read输入回显到屏幕。And-d R是定界符 - 假输入以 结尾R,而不是您可能期望的换行符。
    • column="$(echo "$foo" | cut -d';' -f2)"从响应中提取列号(即跳过行号)并将结果存储在column
    • test "$column" "!=" 1 && { tput smso; echo "%"; tput rmso; }如果当前列号不是 1,则打印百分号(和换行符)。命令打开“tput脱颖而出模式” - 这应该使%脱颖而出更多 - 可能是粗体或可能通过反转背景和前景色。
    • 2

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve