From c9e50f28d9f0444b494eda496190d22b37a8e718 Mon Sep 17 00:00:00 2001 From: gandc Date: Thu, 5 Dec 2024 17:35:33 +0300 Subject: [PATCH] fix stderr output to file --- script | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/script b/script index 7475c12..2ac2399 100644 --- a/script +++ b/script @@ -40,6 +40,24 @@ list_processes() { ps -eo pid,comm --sort=pid } +# Ручная обработка аргументов для раннего перенаправления +for ((i = 1; i <= $#; i++)); do + if [[ ${!i} == "-e" ]]; then + next=$((i + 1)) + ERR_FILE=${!next} + break + fi +done + +# Настройка вывода ошибок +if [[ -n "$ERR_FILE" ]]; then + if ! check_path "$ERR_FILE"; then + exit 1 + fi + echo "Redirecting errors to $ERR_FILE" >&2 + exec 2>"$ERR_FILE" +fi + # Парсинг аргументов через getopts while getopts ":hupl:e:" opt; do case "$opt" in @@ -81,13 +99,6 @@ main() { exec 1>"$LOG_FILE" fi - if [[ -n "$ERR_FILE" ]]; then - if ! check_path "$ERR_FILE"; then - exit 1 - fi - exec 2>"$ERR_FILE" - fi - # Выполнение запрошенных действий if [[ $SHOW_USERS -eq 1 && $SHOW_PROCESSES -eq 1 ]]; then echo "Error: Cannot use both -u and -p options simultaneously." >&2