是否可以通过前一个案例来运行下一个 PS3 案例?

是否可以通过前一个案例来运行下一个 PS3 案例?

假设我有这样的脚本:

#!/bin/bash
PS3='Select option: '
options=("Option one" "Option two")
select opt in "${options[@]}"
do
    case $opt in
    "Option one")
    # few lines of code
    if [ "check that code did everything it was supposed to do" ]
        then
        echo "Completed"
        else
        echo "Something went wrong"
    fi
    ;;

    "Option two")
    # more code
    ;;
    esac
done

现在是否可以将行更改echo "Something went wrong"为立即运行的命令Option two而无需再次显示 PS3 菜单?

答案1

你要找的是“fall-through”,在 bash 的case语句中,fall-through 是使用;&而不是 来完成的;;。但是,你不能有条件地 fall-through(也就是说,你不能;&在块中间插入if)。我建议你总是 fall-through,continue如果代码执行成功:

case $opt in
"Option one")
# few lines of code
if [ "check that code did everything it was supposed to do" ]
then
    echo "Completed"
    continue
else
    echo "Something went wrong"
fi
;&

"Option two")
# more code
;;
esac

相关内容