【问题标题】:Having difficulty figuring out how to pass wild card strings arguments to a bash function难以弄清楚如何将通配符字符串参数传递给 bash 函数
【发布时间】:2020-07-23 20:06:16
【问题描述】:

我有一个我经常使用的 find-grep 命令。在过去的两年里,我一直在策划它并添加更多的东西以使其更有用。最初我把它放在我桌面上的一个文本文件中。我曾经在需要时复制粘贴命令。去年某个时候,我想到将它直接添加到我的 ~/.bashrc 文件中。这是我想出的代码:

findg() {
    if [ "$#" -lt 2 ]; then
        echo "args: (1) File pattern (2) Args to grep"
        return
    fi
    pattern=$1
    file_pattern=$(basename pattern)
    directory_path=$(dirname pattern)

    # Remove the first argument from the list, so you will only be left with GREP arguments.
    shift

    find $directory_path -name "$file_pattern" -exec grep -Hn --color=always -A 5 -B 5 --group-separator=\n=======================\n -E $* {} \;| less -R
}

我尝试以各种方式调用该函数,例如:

findg "$PWD/source_dir/'*.py'" "'special_function\(self'"
findg $PWD/source_dir/"'*.py'" "'special_function\(self'"
findg $PWD/source_dir/"*.py" "'special_function\(self'"
findg $PWD/source_dir/"\*.py" "'special_function\(self'"

在几乎所有情况下,它似乎首先扩展通配符,然后将扩展的输出传递给我的函数调用。如何将字符串传递给我的 bash 函数。

抱歉,如果我对自己想要做什么一无所知,我是 Bash 脚本的新手。

【问题讨论】:

    标签: bash shell grep find


    【解决方案1】:

    只要您在调用findg 时正确引用了该模式,就不需要在该模式中添加额外的引号

    findg "$PWD/source_dir/*.py" "special_function\(self"
    

    不过,您的函数中的引用确实需要针对正则表达式进行修复。

    findg() {
        if [ "$#" -lt 2 ]; then
            echo "args: (1) File pattern (2) Args to grep"
            return
        fi
        pattern=$1
        file_pattern=$(basename "$pattern")
        directory_path=$(dirname "$pattern")
    
        # Remove the first argument from the list, so you will only be left with GREP arguments.
        shift
    
        find "$directory_path" -name "$file_pattern" \
             -exec grep -Hn --color=always -A 5 -B 5 \
                   --group-separator="\n=======================\n" \
                   -E "$@" {} \; | less -R
    }
    

    【讨论】:

    • findg "service/*.cpp" "SpecialRequest" 基本名称:额外的操作数 'service/special.cpp' 尝试使用 'basename --help' 获取更多信息。 find: 'service\nservice\nservice\nservice\nservice\nservice\nservice\nservice\nservice\nservice': No such file or directory 还在扩展第一个参数中的通配符。
    • 您还需要在basenamedirname 调用中将pattern(文字字符串)替换为"$pattern"(正确引用的变量引用)。
    猜你喜欢
    • 2022-07-16
    • 2017-10-24
    • 2013-10-27
    • 2020-03-28
    • 2015-02-09
    • 1970-01-01
    • 2011-05-12
    • 2016-09-15
    相关资源
    最近更新 更多