регистрация блоков кода для регистрации файлов в bash - PullRequest
0 голосов
/ 12 июля 2011

У меня огромный сценарий bash, и я хочу записать определенные блоки кода в определенные & небольшие файлы журнала (вместо одного огромного файла журнала).

У меня есть два следующих метода:

# in this case, 'log' is a bash function

# Using code block & piping
{
# ... bash code ...
} | log "file name"

# Using Process Substitution
log "file name" < <(
     # ... bash code ...
)

Оба метода могут помешать правильному выполнению сценария bash, например, при присвоении значений переменной (например, представленная проблема здесь ).
Как вы предлагаете вести журналвывод команд на лог файлы?


Редактировать: Это то, что я пытался сделать (помимо многих других вариантов), но не работает должным образом:

function log()
{
    if [ -z "$counter" ]; then
        counter=1
        echo "" >> "./General_Log_File" # Create the summary log file
    else
        (( ++counter ))
    fi
    echo "" > "./${counter}_log_file"   # Create specific log file

    # Display text-to-be-logged on screen & add it to the summary log file
    #  & write text-to-be-logged to it's corresponding log file
    exec 1> >(tee "./${counter}_log_file" | tee -a "./General_Log_File") 2>&1
}

log # Logs the following code block
{
    # ... Many bash commands ...
}

log # Logs the following code block
{
    # ... Many bash commands ...
}

Результатыисполнений может быть разным: иногда создаются файлы журнала, а иногда нет (что приводит к ошибке).

Ответы [ 2 ]

1 голос
/ 12 июля 2011

Вы можете попробовать что-то вроде этого:

function log()
{
    local logfile=$1
    local errfile=$2
    exec > $logfile
    exec 2> $errfile    # if $errfile is not an empty string
}

log $fileA $errfileA
echo stuff
log $fileB $errfileB
echo more stuff

Это перенаправит все stdout / stderr из текущего процесса в файл без каких-либо подпроцессов.

Редактировать: Ниже может быть хорошим решением, но не проверено:

pipe=$(mktemp)
mknod $pipe p
exec 1>$pipe

function log()
{
    if ! [[ -z "$teepid2" ]]; then
        kill $teepid2
    else
        tee <$pipe general_log_file &
        teepid1=$!
        count=1
    fi

    tee <$pipe ${count}_logfile &
    teepid2=$!
    (( ++count ))
}

log
echo stuff
log
echo stuff2

if ! [[ -z "$teepid1" ]]; then kill $teepid1; fi
0 голосов
/ 14 июля 2011

Благодаря Sahas мне удалось найти следующее решение:

function log()
{
    [ -z "$counter" ] && counter=1 || (( ++counter ))

    if [ -n "$teepid" ]; then
        exec 1>&- 2>&-  # close file descriptors to signal EOF to the `tee`
                #  command in the bg process
        wait $teepid # wait for bg process to exit
    fi
    # Display text-to-be-logged on screen and
    #  write it to the summary log & to it's corresponding log file
    ( tee "${counter}.log" < "$pipe" | tee -a "Summary.log" 1>&4 ) &
    teepid=$!
    exec 1>"$pipe" 2>&1 # redirect stdout & stderr to the pipe
}

# Create temporary FIFO/pipe
pipe_dir=$(mktemp -d)
pipe="${pipe_dir}/cmds_output"
mkfifo "$pipe"
exec 4<&1   # save value of FD1 to FD4

log # Logs the following code block
{
    # ... Many bash commands ...
}

log # Logs the following code block
{
    # ... Many bash commands ...
}

if [ -n "$teepid" ]; then
    exec 1>&- 2>&-  # close file descriptors to signal EOF to the `tee`
            #  command in the bg process
    wait $teepid # wait for bg process to exit
fi

Работает - проверял.

Ссылки:

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...