processes func switch to getopt
This commit is contained in:
@@ -39,13 +39,36 @@
|
||||
# Usage: ppg <string>
|
||||
ppg()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
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
|
||||
local PARSED
|
||||
|
||||
PARSED=$(getopt -o h --long help -n 'ppg' -- "$@")
|
||||
# shellcheck disable=SC2181 # getopt return code is checked immediately after
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"ppg --help\" to display usage."
|
||||
return 1
|
||||
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
|
||||
disp E "Usage: ppg <string>"
|
||||
return 1
|
||||
@@ -81,13 +104,36 @@ export -f ppg
|
||||
# Usage: ppu <username>
|
||||
ppu()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
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
|
||||
local PARSED
|
||||
|
||||
PARSED=$(getopt -o h --long help -n 'ppu' -- "$@")
|
||||
# shellcheck disable=SC2181 # getopt return code is checked immediately after
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"ppu --help\" to display usage."
|
||||
return 1
|
||||
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
|
||||
disp E "Usage: ppu <username>"
|
||||
return 1
|
||||
@@ -106,13 +152,36 @@ export -f ppu
|
||||
# Usage: ppn <command_name>
|
||||
ppn()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
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
|
||||
local PARSED
|
||||
|
||||
PARSED=$(getopt -o h --long help -n 'ppn' -- "$@")
|
||||
# shellcheck disable=SC2181 # getopt return code is checked immediately after
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"ppn --help\" to display usage."
|
||||
return 1
|
||||
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
|
||||
disp E "Usage: ppn <command_name>"
|
||||
return 1
|
||||
@@ -133,13 +202,36 @@ export -f ppn
|
||||
# Usage: ppid <process_name [process_name2 ...]>
|
||||
gpid()
|
||||
{
|
||||
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
|
||||
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
|
||||
local PARSED
|
||||
|
||||
PARSED=$(getopt -o h --long help -n 'gpid' -- "$@")
|
||||
# shellcheck disable=SC2181 # getopt return code is checked immediately after
|
||||
if [[ $? -ne 0 ]]; then
|
||||
disp E "Invalid options, use \"gpid --help\" to display usage."
|
||||
return 1
|
||||
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
|
||||
disp E "Usage: gpid <process_name [process_name2 ...]>"
|
||||
return 1
|
||||
@@ -190,10 +282,19 @@ export -f gpid
|
||||
# Usage: ku <username1 [username2 ...]>
|
||||
ku()
|
||||
{
|
||||
local PARSED
|
||||
local dry_run=0
|
||||
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
|
||||
-h|--help)
|
||||
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-s, --signal SIG\tSignal to send (overrides KU_DEFAULT_SIGNAL)\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
|
||||
;;
|
||||
-n|--dry-run)
|
||||
@@ -211,21 +312,26 @@ ku()
|
||||
shift
|
||||
;;
|
||||
-s|--signal)
|
||||
if [[ -z "${2:-}" || "$2" == -* ]]; then
|
||||
disp E "--signal requires a value."
|
||||
return 1
|
||||
fi
|
||||
signal_opt=(-s "$2")
|
||||
shift 2
|
||||
;;
|
||||
--signal=*)
|
||||
if [[ -z "${1#*=}" ]]; then
|
||||
disp E "--signal requires a value."
|
||||
return 1
|
||||
fi
|
||||
signal_opt=(-s "${1#*=}")
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
-*)
|
||||
signal_opt=("$1")
|
||||
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:]]*)
|
||||
signal_opt=("$1")
|
||||
shift
|
||||
@@ -284,10 +390,19 @@ export -f ku
|
||||
# Usage: kt <pid> [kill_options]
|
||||
kt()
|
||||
{
|
||||
local PARSED
|
||||
local dry_run=0
|
||||
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
|
||||
-h|--help)
|
||||
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-s, --signal SIG\tSignal to send to process tree\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
|
||||
;;
|
||||
-n|--dry-run)
|
||||
@@ -305,21 +420,26 @@ kt()
|
||||
shift
|
||||
;;
|
||||
-s|--signal)
|
||||
if [[ -z "${2:-}" || "$2" == -* ]]; then
|
||||
disp E "--signal requires a value."
|
||||
return 1
|
||||
fi
|
||||
pre_kill_opts+=(-s "$2")
|
||||
shift 2
|
||||
;;
|
||||
--signal=*)
|
||||
if [[ -z "${1#*=}" ]]; then
|
||||
disp E "--signal requires a value."
|
||||
return 1
|
||||
fi
|
||||
pre_kill_opts+=(-s "${1#*=}")
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
-*)
|
||||
pre_kill_opts+=("$1")
|
||||
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:]]*)
|
||||
pre_kill_opts+=("$1")
|
||||
shift
|
||||
@@ -328,6 +448,10 @@ kt()
|
||||
shift
|
||||
break
|
||||
;;
|
||||
-*)
|
||||
disp E "Unknown option: $1, use \"kt --help\" to display usage."
|
||||
return 1
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
|
||||
Reference in New Issue
Block a user