bash - 运行 shell 脚本时如何解决 "shift: can' t shift the many"错误?

标签 bash shell ubuntu jmeter scripting-language

我有一个具有不同方法的 shell 脚本。脚本是

    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

当我尝试使用 ./jmeter_script.sh start abc.jmx log.jtl 运行脚本时,我收到一条错误消息:./jmeter_script.sh: 19: shift: can' t 移动那么多 然后退出。我在 ubuntu 服务器上运行我的脚本。我已尝试解决此问题,但我找不到此错误的任何正确链接。有人请帮忙。

最佳答案

那些是函数,不是方法。不要在 OOD 术语不适用的地方过度应用它。

避免移位错误的最好方法是在参数用完时不要移位。在您移动之前检查 $# 的值。

如果不指定很多标志,解析 ps -ef 输出会更容易。您只需要 PID 和 PNAME。

关于bash - 运行 shell 脚本时如何解决 "shift: can' t shift the many"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38216468/

相关文章:

bash - 为什么非交互式 Bash shell 中的别名不起作用

unix - 识别并删除 UNIX 中的空字符

c++ - 在MinGW的Shell中编译libtcod时出现 “Error 1”

linux - 命令源码

c++ - 我无法在 Debian(Ubuntu) 中运行在 Redhat(Centos) 中运行的 C++ 程序

bash - wget 文件中随机行的链接

bash - 如何在没有进一步用户交互的情况下使用 sudo 在单独的终端中运行命令

bash - 从变量末尾删除 '/' - bash

linux - 当每行不以数字开头时在 shell 中进行数字排序

python - 如何在 Ubuntu 中删除 Python2?