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
    • 最新
    • 标签
主页 / unix / 问题 / 416277
Accepted
yukashima huksay
yukashima huksay
Asked: 2018-01-11 21:28:51 +0800 CST2018-01-11 21:28:51 +0800 CST 2018-01-11 21:28:51 +0800 CST

如何在 bash 中制作一个特殊的可扩展短语?

  • 772

我发现自己<command> --help | grep <feature>每天都在做非常非常频繁的事情。我想知道是否有可能使类似的东西^^扩展到"--help | grep"然后我这样做:

ls ^^ size

这将执行以下操作:

ls --help | grep size
bash bash-expansion
  • 5 5 个回答
  • 1032 Views

5 个回答

  • Voted
  1. tgwtdt
    2018-01-11T21:31:16+08:002018-01-11T21:31:16+08:00

    您可以为此使用 bash 函数:

    将以下内容放入您的 ~/.bashrc 中:

    qh() {
        type -all "$1" ; { man "$1" || "$1" --help ;} | egrep -i -- "$2"
    }
    

    当您保存您的bashrc 操作时source ~/.bashrc,您可以执行以下操作:

    $ qh ls size
          --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                                   '--block-size=M' prints sizes in units of
      -h, --human-readable       with -l and/or -s, print human readable sizes
      -s, --size                 print the allocated size of each file, in blocks
      -S                         sort by file size, largest first
          --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
      -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
    
    • 17
  2. Best Answer
    Stéphane Chazelas
    2018-01-11T23:06:40+08:002018-01-11T23:06:40+08:00

    使用zsh,您将使用全局别名:

    $ alias -g '^^=--help|grep --color -i'
    $ ls ^^ size
         --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                                  '--block-size=M' prints sizes in units of
                                  1,048,576 bytes; see SIZE format below
     -h, --human-readable       with -l and/or -s, print human readable sizes
     -s, --size                 print the allocated size of each file, in blocks
     -S                         sort by file size, largest first
         --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
     -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
    The SIZE argument is an integer and optional unit (example: 10K is 10*1024)
    

    使用bash,您可以使用历史扩展,这是在 shell 语法解析中足够早发生的一种,它可以用于替换管道:

    1. 使用您想要替换的文本和您不太可能使用的特殊字符来填充历史记录(就像£我的键盘上碰巧出现的这里一样):

       $ --help $(: £)|grep
       bash: --help: command not found
       Usage: grep [OPTION]... PATTERN [FILE]...
       Try 'grep --help' for more information.
      
    2. 然后使用历史扩展来检索:

      $ ls !?£? size
      ls --help $(: £)|grep size
           --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,
                                '--block-size=M' prints sizes in units of
       -h, --human-readable   with -l and/or -s, print human readable sizes
       -s, --size             print the allocated size of each file, in blocks
       -S                     sort by file size, largest first
           --sort=WORD        sort by WORD instead of name: none (-U), size (-S),
       -T, --tabsize=COLS     assume tab stops at each COLS instead of 8
      

    或者你可以readline扩展--help|grep一些按键或按键序列。对于bash仅适用于(而不是其他应用程序,如gdb使用 readline),您可以使用bindbash 内置命令,它是bash配置的 API readline,例如在您的~/.bashrc:

    bind '"^^": "--help|grep "'
    

    或添加到您的~/.inputrc(readline 的配置文件):

    $if Bash
    "^^": "--help|grep "
    $endif
    

    (还有其他类似rc或es使用 readline 的 shell,并且在哪里进行该绑定可能有意义但 AFAICT,它们rl_readline_name在调用之前不会设置变量,readline因此您将无法$if为它们添加一些语句(它们会other像所有应用程序一样显示使用 readline 而不告诉它他们的应用程序名称))。

    请注意,您需要^在第一个之后的半秒内(默认情况下)输入第二个才能进行替换。

    • 15
  3. Alex Stragies
    2018-01-12T00:34:00+08:002018-01-12T00:34:00+08:00

    您可以使用 readline 绑定:

    添加一行

    "^^": "--help | grep "
    

    到你的 ~/.inputrc

    然后在你的任期内按^X ^R,绑定将被激活。

    键控ls ^^现在将导致ls --help | grep.

    • 8
  4. Gaultheria
    2018-01-12T19:15:34+08:002018-01-12T19:15:34+08:00

    用于less查看帮助信息

    您可能会发现查看与您的搜索查询匹配的行的周围上下文很有用。

    hh () { "${1}" --help | less -p "${2}" ; }
    

    调用此bash函数的语法类似于qh@tgwtdt 答案中的函数,第一个参数是要检查的命令,第二个参数是搜索词。例如:

    hh ls size
    
    hh ls "symbolic link"
    

    这将在 中打开完整的帮助消息less,突出显示搜索词的每个实例,并滚动到搜索词的第一个实例。然后,您可以按n向前滚动到包含搜索词的下一行,n再次滚动到下一行,依此类推。要滚动回上一个实例,请按N。使用Home、End、Page Up、Page Down、Up Arrow和Down Arrow键进行一般导航。按q或Q退出less并返回命令行。

    • 5
  5. Joe
    2018-01-13T23:38:59+08:002018-01-13T23:38:59+08:00

    我喜欢@tgwtdt 的解决方案,所以我对其进行了一些改进。

    这做同样的事情,但做了一点处理错误并尝试处理内置函数。

    qh 使用 () 而不是 {} 所以 qh1() 和 out 是本地的(在子shell中)。

    function qh () (
        function qh1 () {
          out="$(help "$1" 2>&1 )"
          [ $? -ne 0 ] && return 1
          echo "$out"
        }
    
        type -all "$1" ; { qh1 "$1" || "$1" --help 2>/dev/null || man "$1" 2>/dev/null ;} | egrep -i -- "$2"
    ) 
    
    • 3

相关问题

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

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

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve