Grep multiple bash parameters

后端 未结 2 1376
再見小時候
再見小時候 2021-01-24 10:38

I\'m writing a bash script which shall search in multiple files.

The problem I\'m encountering is that I can\'t egrep an undetermined number of variables passed as param

2条回答
  •  北海茫月
    2021-01-24 11:12

    A safe eval could be a good solution

    #!/bin/bash
    
    if [[ $# -gt 0 ]]; then
        TEMP=("grep" "-e" "\"\$1\"" "*")
        for (( I = 2; I <= $#; ++I )); do
            TEMP=("${TEMP[@]}" "|" "egrep" "-e" "\"\$${I}\"")
        done
        eval "${TEMP[@]}"
    fi
    

    To run it:

    bash script.sh A B C
    

提交回复
热议问题