Shell脚本:菜单驱动的脚本有多个选项

时间:2012-09-17 18:25:42

标签: shell

我需要一些关于菜单驱动的shell脚本的建议。我希望此脚本的最终用户能够在提示输入选项时提供多个选项。用户还可以输入1个选项,或者他/她可以输入多个选项。

例如。该脚本应该能够处理这两个选项:

Please enter any option or options between [1 - 4]: 1 2 3 

Please enter any option or options between [1 - 4]: 1

请让我知道如何更改我的脚本以反映这一点。

以下是我的脚本代码供您参考:

#!/bin/sh

while :
do
 clear
 echo " ******** Task performing script ******** "
 echo "1. task1 "
 echo
 echo "2. task2 "
 echo
 echo "3. task3"
 echo
 echo "4. Exit"
 echo
 echo -n "Please enter any option or options between [1 - 4]"
     read opt;
 case $opt in
1)
 echo "Performing task1 . . . .";
 for i in `cat test`
 do
 ...............
 ...............
 done;;
2)
 echo "Performing task2 . . . .";
 for i in `cat test`
 do
 ...............
 ...............
 done;;
3)
 echo "Performing task3 . . . .";
 for i in `cat test`
 do
 ...............
 ...............
 done;;
4)
 echo "Bye $USER";
 exit 1;;
*)
 echo "$opt is an invaild option";
 echo "Press [enter] key to continue. . .";
 read enterKey;;
  esac
done

1 个答案:

答案 0 :(得分:2)

case语句包装在迭代的for循环中 超过read语句中的值。

while : do;
    # print menu
    read opts

    for opt in $opts; do  # To allow for multiple space-separated choices

        case $opt in
        ...
        esac
    done
done