Правило udev для запуска сценария оболочки работает на полпути к выполнению сценария, но не завершается

Я включил решение pcregrepи решение python.

Многострочное решение Grep

Если у вас установлено pcregrep, вы можете использовать многострочный шаблон, такой как ^.*FOO.*$\n?(^.*\S.*$\n?)*, например.:

pcregrep -M '^.*FOO.*$\n?(^.*\S.*$\n?)*' test.txt

Подвыражение ^.*FOO.*$\n?будет соответствовать любой строке, содержащей строку FOO, а подвыражение (^.*\S.*$\n?)*будет соответствовать любому количеству последующих строк, содержащих не -пробельный символ.

Решение Python

Вот скрипт Python, который должен делать то, что вы хотите:

#!/usr/bin/env python3
# -*- encoding: utf8 -*-
"""grep_follow.py

Search a text file for a pattern,
and output that pattern and the
non-empty lines which immediately follow it.
"""

import re
import sys

# Get the search pattern and the input file as command-line arguments
pattern = sys.argv[1]
input_file = sys.argv[2]

# Set a flag to determine whether or not to output the current line
print_flag = False

with open(input_file, "r") as _input_file:

    # Iterate over the lines of the input file
    for line in _input_file:

        # Remove trailing whitespace
        line = line.rstrip()

        # If the line is empty, stop producing output
        if not line.strip():
            print_flag = False

        # If the line matches the search pattern, start producing output
        elif re.search(pattern, line):
            print_flag = True

        # If the print flag is set then output the line
        if print_flag:
            print(line)

Вы бы запустили это так:

$ python grep_follow.py FOO test.txt
this line contains FOO
this line is not blank
This line also contains FOO
This line contains FOO too
Not blank
Also not blank
FOO!
Yet more random text
FOO!
0
06.07.2020, 20:55
1 ответ

Я подозреваю, что проблема в том, что сценарий запускается до создания узла устройства.Попробуйте что-то вроде (, то есть без жесткого кодирования ):

.
#!/bin/bash
mkdir /media/sdb1
(
    while [[ ! -b /dev/sdb1 ]]
    do
        sleep 1
    done
    /bin/mount /dev/sdb1 -o ro,noatime /media/sdb1
) &

Важной частью является фоновая подоболочка.

В зависимости от того, что делает udev, у вас могут возникнуть проблемы с необходимостью что-то делать с stdin, stdout и stderr. Вам также может потребоваться игнорировать SIGHUP или какой-либо другой сигнал (, но, вероятно, не ).

0
18.03.2021, 23:21

Теги

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