#!/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