processes func switch to getopt

This commit is contained in:
fatalerrors
2026-05-21 14:28:40 +02:00
parent 648777a13e
commit 02cd9a853e

View File

@@ -39,13 +39,36 @@
# Usage: ppg <string> # Usage: ppg <string>
ppg() ppg()
{ {
if [[ "$1" == "-h" || "$1" == "--help" ]]; then local PARSED
printf "ppg: Search processes matching the given string.\n\n"
printf "Usage: ppg <string>\n\n" PARSED=$(getopt -o h --long help -n 'ppg' -- "$@")
printf "Options:\n" # shellcheck disable=SC2181 # getopt return code is checked immediately after
printf "\t-h, --help\t\tDisplay this help screen\n" if [[ $? -ne 0 ]]; then
return 0 disp E "Invalid options, use \"ppg --help\" to display usage."
return 1
fi fi
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help)
printf "ppg: Search processes matching the given string.\n\n"
printf "Usage: ppg <string>\n\n"
printf "Options:\n"
printf "\t-h, --help\t\tDisplay this help screen\n"
return 0
;;
--)
shift
break
;;
*)
disp E "Invalid options, use \"ppg --help\" to display usage."
return 1
;;
esac
done
if [[ -z "$1" ]]; then if [[ -z "$1" ]]; then
disp E "Usage: ppg <string>" disp E "Usage: ppg <string>"
return 1 return 1
@@ -81,13 +104,36 @@ export -f ppg
# Usage: ppu <username> # Usage: ppu <username>
ppu() ppu()
{ {
if [[ "$1" == "-h" || "$1" == "--help" ]]; then local PARSED
printf "ppu: List processes owned by a specific user.\n\n"
printf "Usage: ppu <username>\n\n" PARSED=$(getopt -o h --long help -n 'ppu' -- "$@")
printf "Options:\n" # shellcheck disable=SC2181 # getopt return code is checked immediately after
printf "\t-h, --help\t\tDisplay this help screen\n" if [[ $? -ne 0 ]]; then
return 0 disp E "Invalid options, use \"ppu --help\" to display usage."
return 1
fi fi
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help)
printf "ppu: List processes owned by a specific user.\n\n"
printf "Usage: ppu <username>\n\n"
printf "Options:\n"
printf "\t-h, --help\t\tDisplay this help screen\n"
return 0
;;
--)
shift
break
;;
*)
disp E "Invalid options, use \"ppu --help\" to display usage."
return 1
;;
esac
done
if [[ -z "$1" ]]; then if [[ -z "$1" ]]; then
disp E "Usage: ppu <username>" disp E "Usage: ppu <username>"
return 1 return 1
@@ -106,13 +152,36 @@ export -f ppu
# Usage: ppn <command_name> # Usage: ppn <command_name>
ppn() ppn()
{ {
if [[ "$1" == "-h" || "$1" == "--help" ]]; then local PARSED
printf "ppn: List processes by exact command name (no path/parameters).\n\n"
printf "Usage: ppn <command_name>\n\n" PARSED=$(getopt -o h --long help -n 'ppn' -- "$@")
printf "Options:\n" # shellcheck disable=SC2181 # getopt return code is checked immediately after
printf "\t-h, --help\t\tDisplay this help screen\n" if [[ $? -ne 0 ]]; then
return 0 disp E "Invalid options, use \"ppn --help\" to display usage."
return 1
fi fi
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help)
printf "ppn: List processes by exact command name (no path/parameters).\n\n"
printf "Usage: ppn <command_name>\n\n"
printf "Options:\n"
printf "\t-h, --help\t\tDisplay this help screen\n"
return 0
;;
--)
shift
break
;;
*)
disp E "Invalid options, use \"ppn --help\" to display usage."
return 1
;;
esac
done
if [[ -z "$1" ]]; then if [[ -z "$1" ]]; then
disp E "Usage: ppn <command_name>" disp E "Usage: ppn <command_name>"
return 1 return 1
@@ -133,13 +202,36 @@ export -f ppn
# Usage: ppid <process_name [process_name2 ...]> # Usage: ppid <process_name [process_name2 ...]>
gpid() gpid()
{ {
if [[ "$1" == "-h" || "$1" == "--help" ]]; then local PARSED
printf "gpid: Get PID list of the given process name.\n\n"
printf "Usage: gpid <process_name [process_name2 ...]>\n\n" PARSED=$(getopt -o h --long help -n 'gpid' -- "$@")
printf "Options:\n" # shellcheck disable=SC2181 # getopt return code is checked immediately after
printf "\t-h, --help\t\tDisplay this help screen\n" if [[ $? -ne 0 ]]; then
return 0 disp E "Invalid options, use \"gpid --help\" to display usage."
return 1
fi fi
eval set -- "$PARSED"
while true; do
case "$1" in
-h|--help)
printf "gpid: Get PID list of the given process name.\n\n"
printf "Usage: gpid <process_name [process_name2 ...]>\n\n"
printf "Options:\n"
printf "\t-h, --help\t\tDisplay this help screen\n"
return 0
;;
--)
shift
break
;;
*)
disp E "Invalid options, use \"gpid --help\" to display usage."
return 1
;;
esac
done
if [[ -z "$1" ]]; then if [[ -z "$1" ]]; then
disp E "Usage: gpid <process_name [process_name2 ...]>" disp E "Usage: gpid <process_name [process_name2 ...]>"
return 1 return 1
@@ -190,10 +282,19 @@ export -f gpid
# Usage: ku <username1 [username2 ...]> # Usage: ku <username1 [username2 ...]>
ku() ku()
{ {
local PARSED
local dry_run=0 local dry_run=0
local -a signal_opt=() local -a signal_opt=()
while [[ $# -gt 0 ]]; do PARSED=$(getopt -o hns: --long help,dry-run,signal: -n 'ku' -- "$@")
# shellcheck disable=SC2181 # getopt return code is checked immediately after
if [[ $? -ne 0 ]]; then
disp E "Invalid options, use \"ku --help\" to display usage."
return 1
fi
eval set -- "$PARSED"
while true; do
case "$1" in case "$1" in
-h|--help) -h|--help)
printf "ku: Kill all processes owned by the given users.\n\n" printf "ku: Kill all processes owned by the given users.\n\n"
@@ -203,7 +304,7 @@ ku()
printf "\t-n, --dry-run\t\tDisplay commands without executing them\n" printf "\t-n, --dry-run\t\tDisplay commands without executing them\n"
printf "\t-s, --signal SIG\tSignal to send (overrides KU_DEFAULT_SIGNAL)\n" printf "\t-s, --signal SIG\tSignal to send (overrides KU_DEFAULT_SIGNAL)\n"
printf "\t --signal=SIG\tSame as above\n" printf "\t --signal=SIG\tSame as above\n"
printf "\t -SIG / -NUM\tSignal format compatible with kill\n" printf "\t -SIG / -NUM\t\tSignal format compatible with kill\n"
return 0 return 0
;; ;;
-n|--dry-run) -n|--dry-run)
@@ -211,21 +312,26 @@ ku()
shift shift
;; ;;
-s|--signal) -s|--signal)
if [[ -z "${2:-}" || "$2" == -* ]]; then
disp E "--signal requires a value."
return 1
fi
signal_opt=(-s "$2") signal_opt=(-s "$2")
shift 2 shift 2
;; ;;
--signal=*) --)
if [[ -z "${1#*=}" ]]; then shift
disp E "--signal requires a value." break
return 1 ;;
fi -*)
signal_opt=(-s "${1#*=}") signal_opt=("$1")
shift shift
;; ;;
*)
break
;;
esac
done
# Accept kill-style signal forms not handled by getopt, before usernames.
while [[ $# -gt 0 ]]; do
case "$1" in
-[0-9]*|-SIG*|-[[:alpha:]]*) -[0-9]*|-SIG*|-[[:alpha:]]*)
signal_opt=("$1") signal_opt=("$1")
shift shift
@@ -284,10 +390,19 @@ export -f ku
# Usage: kt <pid> [kill_options] # Usage: kt <pid> [kill_options]
kt() kt()
{ {
local PARSED
local dry_run=0 local dry_run=0
local -a pre_kill_opts=() local -a pre_kill_opts=()
while [[ $# -gt 0 ]]; do PARSED=$(getopt -o hns: --long help,dry-run,signal: -n 'kt' -- "$@")
# shellcheck disable=SC2181 # getopt return code is checked immediately after
if [[ $? -ne 0 ]]; then
disp E "Invalid options, use \"kt --help\" to display usage."
return 1
fi
eval set -- "$PARSED"
while true; do
case "$1" in case "$1" in
-h|--help) -h|--help)
printf "kt: Kill all children of a process then the process (kill tree).\n\n" printf "kt: Kill all children of a process then the process (kill tree).\n\n"
@@ -297,7 +412,7 @@ kt()
printf "\t-n, --dry-run\t\tDisplay kill commands without executing them\n" printf "\t-n, --dry-run\t\tDisplay kill commands without executing them\n"
printf "\t-s, --signal SIG\tSignal to send to process tree\n" printf "\t-s, --signal SIG\tSignal to send to process tree\n"
printf "\t --signal=SIG\tSame as above\n" printf "\t --signal=SIG\tSame as above\n"
printf "\t -SIG / -NUM\tSignal format compatible with kill\n" printf "\t -SIG / -NUM\t\tSignal format compatible with kill\n"
return 0 return 0
;; ;;
-n|--dry-run) -n|--dry-run)
@@ -305,21 +420,26 @@ kt()
shift shift
;; ;;
-s|--signal) -s|--signal)
if [[ -z "${2:-}" || "$2" == -* ]]; then
disp E "--signal requires a value."
return 1
fi
pre_kill_opts+=(-s "$2") pre_kill_opts+=(-s "$2")
shift 2 shift 2
;; ;;
--signal=*) --)
if [[ -z "${1#*=}" ]]; then shift
disp E "--signal requires a value." break
return 1 ;;
fi -*)
pre_kill_opts+=(-s "${1#*=}") pre_kill_opts+=("$1")
shift shift
;; ;;
*)
break
;;
esac
done
# Accept kill-style signal forms not handled by getopt, before the PID.
while [[ $# -gt 0 ]]; do
case "$1" in
-[0-9]*|-SIG*|-[[:alpha:]]*) -[0-9]*|-SIG*|-[[:alpha:]]*)
pre_kill_opts+=("$1") pre_kill_opts+=("$1")
shift shift
@@ -328,6 +448,10 @@ kt()
shift shift
break break
;; ;;
-*)
disp E "Unknown option: $1, use \"kt --help\" to display usage."
return 1
;;
*) *)
break break
;; ;;