简体   繁体   English

解析而不解析标志和参数时回显信息

[英]echo information when parsing and not parsing flags and argument

#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 $@

This is what I write so far, I want to have something like 这是我到目前为止所写的,我想要类似

./KillByName -g 24601 ./KillByName -g 24601
​graceful kill is present 优美的杀戮存在

or 要么

./KillByName -g ./KillByName -g
​Usage: KillByName [-g] -n or KillByName [-g] 用法:KillByName [-g] -n或KillByName [-g]

or 要么

./KillByName -g -n bash ./KillByName -g -n bash
graceful kill is present 优美的杀戮存在
process name is present 进程名称存在

essentially, if there is -g, then it shows it is gracefully killed, and with a name. 本质上,如果存在-g,则表明它已被优雅地杀死,并带有名称。 If there is -n, then it says the name exits and with a name. 如果存在-n,则表示名称已退出并带有名称。 I found my script can print the message of whether graceful kill present or name present, but cannot print the mistake of $USAGE. 我发现我的脚本可以打印出是否出现优雅的kill或name的消息,但无法打印$ USAGE的错误。

BTW: this is only for information of usage, not actually program of killing the program 顺便说一句:这仅用于使用的信息,而不是实际上杀死程序的程序

First of all, 首先,

usage()

is not the way you call a function, It should've been 不是您调用函数的方式,应该

usage

But there is a problem with that, you're not passing any arguments to the function usage from main , so it should've been 但这有一个问题,您没有从main传递任何参数给函数usage ,因此应该

usage "$@" # Double quotes to prevent word splitting

Though the term "graceful killing" is a paradox in itself, you could do something like 尽管“优雅杀戮”一词本身就是一个悖论,但您可以做一些类似的事情

while getopts ":gn:" OPT; do
gracekill=0; //s  
case $OPT in  
        g) gracekill=1;;  
        n) name=$OPTARG;;  
        :) echo "$USAGE"  
           exit 1  
        ;;  
        \?) echo "$USAGE"  
            exit 1  
        ;;  
        *) echo "$USAGE"  
           exit 1  
    esac
    done
    echo "$gracekill $name" # Mind double quotes  

Then do this : 然后这样做:

result=$(usage "$@")
if [ ${result:0:1} -eq '1' ]
then
#gracefully kill the application
kill -9  $( pgrep "${result:1}" )
else
#ruthlessly terminate it
kill -15 $( pgrep "${result:1}" )
fi

For more on ${var:offset:length} form, see [ param expansion ] 有关${var:offset:length}格式的更多信息,请参见[参数扩展]

Notes : I assume that you're passing the process names to the function, if you're passing the process number, then you don't need the pgpep ie kill -15 $( pgrep "${result:1}" ) will become kill -15 "${result:1}" and so. 注意:我假设您正在将进程名称传递给该函数,如果您要传递进程号,则不需要pgpepkill -15 $( pgrep "${result:1}" )将变成kill -15 "${result:1}" ,依此类推。 Goodluck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM