Bash - 读取多个选择的案例

时间:2017-07-28 11:11:23

标签: bash switch-statement

我在脚本中使用以下代码。

#!/bin/bash

while true; do
    read -p "Do your Choice: [1] [2] [3] [4] [E]xit: " choice
    case "$choice" in
        [1]* ) echo -e "$choice\n"; break;;
        [2]* ) echo -e "$choice\n"; break;;
        [3]* ) echo -e "$choice\n"; break;;
        [4]* ) echo -e "$choice\n"; break;;
        [Ee]* ) echo "exited by user"; exit;;
        * ) echo "Are you kidding me???";;
    esac
done

我的问题是,如何让脚本接受多个选择。 所以输入如:1,4,会运行案例[1][4]

1 个答案:

答案 0 :(得分:2)

将IFS设置为包含逗号:

IFS=', '

然后在循环中处理选项(注意-a的{​​{1}}标志,以便将输入视为数组:

read