免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2975 | 回复: 9
打印 上一主题 下一主题

新人求教::关于LOOP和CASE的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-18 13:39 |只看该作者 |倒序浏览
刚开始自学script,现在刚学到LOOP

在书上看到一个题目,对于CASE不是很会,请高手指点一下

题目是:
  * Accepts two positive integers from the command line. Verify that both arguments were positive integers.  Enclose this verification inside a loop so that if one of these arguments was not a number it would display a "USAGE" error message and then ask the user for proper input. You should remain in this loop until the user data entry is two positive numbers.
          * Prompts the user through a menu, to select a letter that performs one of the following through a case statement:
              1. Display the sum of the two numbers.
              2. Display the product of the two numbers.
              3. State whether the two individual numbers are ODD or EVEN.
              4. Display the higher of the two numbers
              5. Ask the user if they like the "first" or "second" number best (have them input the word) and output "I agree" (if "first") or "I disagree", based on their choice.
              6. Quit the script (Exit)
          * Prints a friendly message if one of the above 5 choices are not chosen correctly (a, b, c, d or e).

论坛徽章:
0
2 [报告]
发表于 2011-02-18 13:47 |只看该作者
作业要自己做

论坛徽章:
0
3 [报告]
发表于 2011-02-18 14:01 |只看该作者
因为是自学没有老师指点,所以才来问呀

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
4 [报告]
发表于 2011-02-18 14:22 |只看该作者
可以先自己写个,然后拿出来大家一起讨论

论坛徽章:
0
5 [报告]
发表于 2011-02-20 03:57 |只看该作者
本帖最后由 afton 于 2011-02-20 12:57 编辑
  1. #!/bin/bash
  2. clear


  3. until echo $1 | grep -s "^[-+]*[0-9][0-9]*$" && echo $2 | grep -s "^[-+]*[0-9][0-9]*$"
  4. do
  5.   echo "USAGE:You have selected an INVALID integer."
  6.   echo "Please re-enter these two positive integers, seperated by sapce: "
  7. done

  8. echo

  9. # Menu Items and prompt user


  10. cat <<+
  11. MENU
  12. a. Display the sum of the two numbers.
  13. b. Display the product of the two numbers.
  14. c. State whether the two individual numbers are ODD or EVEN.
  15. d. Display the higher of the two numbers
  16. e. Select the number which you like
  17. f. Quit the script (Exit)
  18. +

  19. # Menu Items and prompt user

  20. read -p "Please select options(a,b,c,d,e or f):" choice
  21. until [ $choice = "a" ] || [ $choice = "b" ] || [ $choice = "c" ] || [ $choice = "d" ] || [ $choice = "e" ] || [ $choice = "f" ]
  22. do
  23. clear
  24. echo
  25. cat <<+
  26. MENU
  27. a. Display the sum of the two numbers.
  28. b. Display the product of the two numbers.
  29. c. State whether the two individual numbers are ODD or EVEN.
  30. d. Display the higher of the two numbers
  31. e. Select the number which you like
  32. f. Quit the script (Exit)
  33. +
  34. read -p "Please select options(a,b,c,d,e or f):" choice
  35. done

  36. case $choice in
  37. "a") echo "$1 + $2 = $(($1+$2))"
  38.      ;;
  39. "b") echo "$1 * $2 = $(($1*$2))"
  40.      ;;
  41. "c") if  test $(($1 %2)) -eq 0
  42.        then
  43.            echo "$1 is an EVEN number."
  44.      else
  45.          echo "$1 is an ODD number."
  46.      fi

  47.      if  test $(($2 % 2)) -eq 0
  48.         then
  49.            echo "$2 is an EVEN number."
  50.      else
  51.          echo "$2 is an ODD number."
  52.      fi
  53.      ;;
  54. "d") if test $(($1 - $2)) -gt 0
  55.         then
  56.            echo "$1 is higher."
  57.      elif test $(($1-$2)) -eq 0
  58.         then
  59.            echo "These two numbers are equal."
  60.      else
  61.            echo "$2 is higher."
  62.      fi
  63.      ;;
  64. "e") echo
  65.      read -p "Do you like the first number or second number best (enter "first" or "second")? : " like
  66.      until [ $like = "first" ] || [ $like = "second"  ]
  67.      do
  68.      read -p "Do you like the first number or second number best (enter "first" or "second")? : " like
  69.      done
  70.      if test $like = "first"
  71.         then
  72.            echo "I agree."
  73.      else
  74.         echo "I disagree."
  75.      fi
  76.      ;;
  77. "f") exit
  78.      ;;
  79. esac
复制代码
这是我写的,现在的问题是,在选择完abcd后就直接退出了,没有让用户再做其它选择

还有两个参数在命令行输入,如果输入./lab.bash  2 4 运行时正常

但如果输入./lab.bash  a 3   

请问怎么解决?

论坛徽章:
0
6 [报告]
发表于 2011-02-20 09:36 |只看该作者

论坛徽章:
1
天秤座
日期:2013-10-23 13:20:42
7 [报告]
发表于 2011-02-20 12:18 |只看该作者
bash +x your_bash_script

来进行调试,看看到底在那个环节不是按照你的思路走的。

论坛徽章:
0
8 [报告]
发表于 2011-02-20 12:35 |只看该作者
bash +x your_bash_script

来进行调试,看看到底在那个环节不是按照你的思路走的。
jerryjzm 发表于 2011-02-20 12:18



   这个调试我还没学,
不过现在就是死循环

论坛徽章:
0
9 [报告]
发表于 2011-02-20 13:02 |只看该作者
现在我已经解决了./lab.bash -14 3 的问题了,

但是我输入./lab.bash a 3 或者./lab.bash 3 3.3就进入死循环

还有Menu的部分,我写了循环语句,为什么执行完option就退出了?

论坛徽章:
0
10 [报告]
发表于 2011-02-20 13:31 |只看该作者
不是很清楚,但可以提供个小技巧,如果你不知道死在哪儿,可以在可疑的地方加上一个echo +数字~这样就可以清楚知道程序走到了哪一步~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP