commit 722d22bf2ad80af0861eca57213ba84e59bbd8f0 Author: gandc Date: Tue Oct 1 13:55:26 2024 +0300 first commit diff --git a/zad1-lk.txt b/zad1-lk.txt new file mode 100644 index 0000000..67d0324 --- /dev/null +++ b/zad1-lk.txt @@ -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