【问题标题】:less: filter out pattern passed as command line argument + follow file via bash function少:过滤掉作为命令行参数传递的模式+通过bash函数跟随文件
【发布时间】:2021-05-01 20:16:31
【问题描述】:

我正在尝试创建一个 bash 函数,该函数将使用 less 应用模式并使用传递给函数的参数跟踪文件

my_less_function() {
        if [ -z "$1" ]
        then
                # if no arg
                less +F /var/log/my.log
        else
                # else, filter out the arg
                less +$'&!'$1'\nF' /var/log/my.log
        fi
}

我的问题是我无法让 arg 在 else 块中正确替换

my_less_function MY_VALUE 显示 Non-match &/MY_VALUE\nF 更少

看起来它正在连接参数和\nF,但\nF 应该触发跟随命令而不是被解释为参数的一部分

有什么想法吗?

【问题讨论】:

    标签: bash shell command-line-arguments


    【解决方案1】:

    错误:less +$'&!'$1'\nF' /var/log/my.log


    正确:less +$'&!'${1}$'\nF' /var/log/my.log

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2019-12-07
      • 2021-11-29
      相关资源
      最近更新 更多