first commit

This commit is contained in:
gandc 2024-10-01 13:55:26 +03:00
commit 722d22bf2a
Signed by: gandc
GPG Key ID: 9F77B03D43C42CB4

79
zad1-lk.txt Normal file
View File

@ -0,0 +1,79 @@
#!/bin/bash
list_users() {
awk -F: '{print $1, $6}' /etc/passwd | sort
}
list_processes() {
ps -eo pid,comm --sort=pid
}
show_help() {
echo "Usage: $0 [options]."
echo "Options:"
echo " -u, --users Display a list of users and their home directories"
echo " -p, --processes Display a list of running processes"
echo " -l, --log PATH Redirect output to a specified file"
echo " -e, --errors PATH Redirect error output to a specified file"
echo " -h, --help Show this message and terminate"
}
check_path() {
local path=$1
if [ ! -w "$path" ]; then
echo "Error: No access to write to $path" >&2
exit 1
fi
}
log_output() {
local path=$1
check_path "$path"
exec >"$path"
}
log_errors() {
local path=$1
check_path "$path"
exec 2>"$path"
}
ARGS=$(getopt -o upl:e:h --long users,processes,log:,errors:,help -n "$0" -- "$@")
if [ $? -ne 0 ]; then
echo "Error: Incorrect input" >&2
exit 1
fi
eval set -- "$ARGS"
while true; do
case "$1" in
-u|--users)
list_users
shift
;;
-p|--processes)
list_processes
shift
;;
-l|--log)
log_output "$2"
shift 2
;;
-e|--errors)
log_errors "$2"
shift 2
;;
-h|--help)
show_help
exit 0
;;
--)
shift
break
;;
*)
echo "Unknown parameter $1" >&2
exit 1
;;
esac
done