fix stderr output to file

This commit is contained in:
gandc 2024-12-05 17:35:33 +03:00
parent e4454f77c2
commit c9e50f28d9
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4

25
script
View File

@ -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