#function: usage
#description: 1. parse command line arguments
# 2. for illegal usages, print usage message and exit 1
# 3. otherwise, communicate to main() what flags are set
function usage {
while getopts ":gn:" OPT; do
case $OPT in
g) ;;
n) name=$OPTARG;;
:) echo "$USAGE"
exit 1
;;
\?) echo "$USAGE"
exit 1
;;
*) echo "$USAGE"
exit 1
esac
done
shift $(($OPTIND + 1))
}
#function: main
#description: For Part 1:
# 1. use usage() to parse command line arguments
# 2. echo corresponding messages for each flag that is set
#
# For Part 2:
# Kill processes based on the case return by `usage`
function main {
# TODO change the condition so that this line prints when '-g' is set
usage()
if [ ! -z "g" ]; then
echo graceful kill is present
fi
# TODO change the condition so that this line prints when '-n' is set
if [ ! -z "n" ]; then
echo process name is present
fi
main [email protected]
这是我写的,到目前为止的时候,我希望能有像回波信息解析和未解析标志和参数
./KillByName -g 24601
优美杀存在
或
./KillByName -g
用法:KillByName [-g] -n或KillByName [-g]
或
./KillByName -g -n庆典
优美杀存在
进程名存在
基本上,如果有-g,那么就说明它是优雅杀害,并用名称。如果有-n,那么它表示该名称退出并带有一个名称。 我发现我的脚本可以打印优雅杀人礼物或名字礼物的消息,但不能打印$ USAGE的错误。
BTW:这仅仅是使用的信息,查杀程序
以及如何使-g选项标志。也就是说,仍然需要一个pid,但使用-g时,该进程会优雅地被杀掉。但是,如果-g退出但pid不存在,它会提供虚假信息? – faker
我建议不要在子shell中运行'usage'函数('result = $(usage“$ @”)') - 它增加了解析其输出的复杂性(取决于参数采用什么形式,这可能会非常棘手),并且其中的exit 1也不会实际退出主脚本,只是子shell。国际海事组织(IMO)将脚本选项解析代码嵌入脚本的主要部分会更清洁,并避免所有这些复杂性。 –