Linux之shell编程基础实验----目录菜单实例

预习Linux实验时遇到

给出shell脚本:

until 
echo 1 list directory
echo 2 change directory
echo 3 edit file
echo 4 delete file
echo 5 exit menu
read choice
test $choice = 5
do
	case $choice in
	1) ls
		;;
	2) read dir
	   cd $dir
	   pwd
	   ;;
	3) read file
	  vim $file
	  ;;
	4) read file
	  rm $file
	  ;;
	q|Q|5) echo "good bye"
	  ;;
	*) echo "illegal option"
	Esac

要求是调试程序使其工作,输入q|Q|5三个中的任何一个时,输出"good bye",并退出菜单。

这段程序比较简单,通过调试运行可以得出这段脚本存在的问题:
1、do……done 所以结尾少了一个done
2、输入5会满足until条件直接跳出,没有good bye
3、输入为空或者空格则流程控制缺少条件

修改之使其符合要求:

until
echo 1 list directory
echo 2 change directory
echo 3 edit file
echo 4 delete file
echo 5 exit menu
read choice
test -z $choice
do
        case $choice in
        1) ls
           ;;
        2) read dir
           cd $dir
           pwd
           ;;
        3) read file
          vim $file
          ;;
        4) read file
          rm $file
          ;;
        q|5|Q) break
          ;;
        *) echo "illegal option"
        esac
done
echo good bye

修改后:
1、until条件为检测参数是否为空,为空跳出循环good bye
2、选中q|5|Q结束循环good bye

你可能感兴趣的:(Linux,shell,脚本编程)