首页 > 解决方案 > 测试 ./script.sh doit 测试不显示真实输出

问题描述

#!/usr/bin/env zsh

doit() {
    if [[ "$1" = "start" ]]; then
        for loc in $(cat all-doc); do
            if ! screen -list | grep -q My-$loc; then
                screen -dmS My-$loc /home/Server -f /home/$loc.cfg
            fi
        done
    elif [[ "$1" = "stop" ]]; then
        for loc in $(cat all-doc); do
            if screen -list | grep -q My-$loc; then
                pkill -f My-$loc;
            fi
        done
    else
        echo "Option: ERROR..."
    fi
}

nothing() {
    if [[ "$1" = "start" ]]; then
        echo "Option: 1"
    elif [[ "$1" = "stop" ]]; then
        echo "Option: 2"
    else
        echo "Option: 3"
    fi
}

case "$2" in
    start)
           "$1" "$2";
           ;;
    stop)
           "$1" "$2";
           ;;
    restart)
           restart;
           ;;
    *)
           echo "Usage: $0 {doit|nothing} {start|stop|restart}"
           exit 1
           ;;
esac

exit 0

输出:

./script.sh:34> case test (start)
./script.sh:34> case test (stop)
./script.sh:34> case test (restart)
./script.sh:34> case test (*)
./script.sh:45> echo 'Usage: ./script.sh {start|stop|restart}'
sage: ./script.sh {start|stop|restart}
./script.sh:46> exit 1

此脚本用于启动和停止并重新启动我的服务器。如果 $2 与两个函数中的 "start" "stop" "restart" 不匹配,则必须调用else但不起作用。好的问题是为什么./script.sh doit test不叫 else echo "Option: ERROR..." whats the sulotion?有没有更好的方法来为 ./script.sh $1 $2 我的意思是 $1 get function 和 $2 get start|stop|restart 做一些事情?

标签: zsh

解决方案


推荐阅读