2

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

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

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
4

1 回答 1

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
于 2012-09-17T18:34:33.460 回答