bash中CLI的正确语法,该语法将接受带空格的参数

while getopts abp: optchar; do
    case "${optchar}" in

    a) # an option
        export OPTION_A=1
        ;;

    b) # Yet another option
        export b=1
        ;;

    p) # An option to supply player names to the function
        IFS=',' read -r -a PLAYER_NAMES <<<"${OPTARG}"
        ;;
    esac
done

for name in "${PLAYER_NAMES[@]}"; do
    echo ${name}

现在,如果为'players'关键字参数提供逗号分隔的列表,则该函数将忽略以空格分隔的参数。例如,如果您运行:

bash testexp.sh -p sherman,wilson,taylor

您将获得

sherman
wilson

但是taylor不会被包括在内,因为他的名字没有被添加到数组变量中。

如何使cli解析器忽略多余的空间,以便输出如下?

sherman
wilson
taylor
cx3232 回答:bash中CLI的正确语法,该语法将接受带空格的参数

使用撇号:

bash testexp.sh -p 'sherman,wilson,taylor'
本文链接:https://www.f2er.com/3077010.html

大家都在问