проблемы с переменными greping в Linux

Ниже приведен код, который я пытаюсь выполнить:

{    
echo "Enter dirname and hit Return"
read input1
echo "Enter a pattern to be searched for in the current directory"
read input2
find /*/${input1}/*/logs/*/*/*/* -name '*.gz' -exec sh -c 'gzip -cd "$0" | grep -- "${input2}"' {} \;
}

в то время как input1 совпадает, но input2, похоже, не совпадает и Я получаю все выходные данные input1 без сопоставления input2.

цель - прочитать все файлы .gz и получить совпадение ключевого слова input2.

0
25.10.2017, 16:58
1 ответ

El script "interno" que llama findno tiene acceso a la variable $input2.

En su lugar, puede hacer

find /*/"$input1"/*/logs/*/*/*/* -name '*.gz' \
    -exec sh -c 'gzip -cd "$1" | grep -e "$0"' "$input2" {} \;

Esto pasa el valor de $input2al script interno y lo hará disponible como $0mientras que el argumento del nombre de archivo será $1.

Alternativamente, simplemente deje que finddescomprima los archivos y filtre la salida de findcomo un todo:

find /*/"$input1"/*/logs/*/*/*/* -name '*.gz' \
    -exec gzip -cd {} + | grep -e "$input2"

Dado que findya ingresa en todos los subdirectorios del directorio superior dado -, probablemente pueda omitir algunos de los nombres de archivo y usar-mindepth 4(en su lugar si su findadmite esta opción ), y agregue -type fpara indicar que solo está interesado en archivos regulares:

find /*/"$input1"/*/logs -mindepth 4 -type f -name '*.gz' \
    -exec gzip -cd {} + | grep -e "$input2"
0
28.01.2020, 04:37

Теги

Похожие вопросы