How to resolve “shift: can't shift that many” error while running a shell script?

旧街凉风 提交于 2019-12-12 18:22:52

问题


I have a shell script with different methods.The script is

    status(){
    PID=$(ps -ef | grep jmeter_script.sh|grep -v grep|awk '{print $2}')
    }

    start(){
    shift
    if [ "$#" -ne 2 ]; then
            exit 1;
    fi
    jmeter.sh -n -t "$1"  -l "$2"
    }

    stop(){
        while true; do
            read -p "Do you really want to stop?" yn
            case $yn in
                [Yy]* ) ps -ef | grep jmeter | grep -v grep | awk '{print $2}'|xargs kill; break;;
                [Nn]* ) exit;;
                * ) echo "Please answer yes or no.";;
            esac
view(){
#some code
}

    ####Main
    while [ "$1" != "" ]; do
    case "$1" in
            start )
                start
                ;;

            stop)
                stop
                ;;

            status)
                status
                ;;
            view)
                view
                ;;
            *)
                echo $"Usage: $0 {start|stop|status|view}"
                exit 1
            esac
        shift
    done

When I try to run the script using ./jmeter_script.sh start abc.jmx log.jtl I get an error saying ./jmeter_script.sh: 19: shift: can't shift that many and then exit. I am running my script on ubuntu server. I have tried to resolve this but I ddin't find any proper link for this error. Someone please help.


回答1:


Those are functions, not methods. Don't go overboard applying OOD terminology where it doesn't begin to apply.

The best way to avoid shift errors is not to shift when you have run out of arguments. Check the value of $# BEFORE you shift.

It would be easier to parse your ps -ef output if you don't specify lots of flags. All you need is PID and PNAME.



来源:https://stackoverflow.com/questions/38216468/how-to-resolve-shift-cant-shift-that-many-error-while-running-a-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!