Chinaunix首页 | 论坛 | 博客
  • 博客访问: 491386
  • 博文数量: 71
  • 博客积分: 1332
  • 博客等级: 少尉
  • 技术积分: 772
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-10 16:25
个人简介

文章分类

全部博文(71)

文章存档

2013年(19)

2012年(9)

2011年(43)

分类: Python/Ruby

2011-04-07 14:20:04



  1. #!/bin/bash
  2. # read-without-new-line.sh
  3. # using \c to get input in the same line
  4. echo -e "Input your operate system:\c"
  5. read answer
  6. function func
  7. {
  8.     echo "Function start ...."
  9.     if [ $answer != `uname` ]
  10. #    if [ $naswer != $(uanme) ] #### is same as above
  11. #    if [ "$answer" != "`uname`" ] #### is same as above
  12.     then
  13.         echo No-$answer
  14.         echo "You don't knowe your operate system!"
  15. #        exit 5
  16.         return 100
  17.     else
  18.         echo OK-$answer
  19.         echo "Hehe, you knowe your operate system!"
  20. #        exit 4
  21.         return 99
  22.     fi
  23. }
  24. echo -----Out put ---
  25. echo Your system is:
  26. echo $(uname)

  27. c=$(func)
  28. echo \$c=func\(\);
  29. echo The value of \$c is:
  30. echo $c

  31. echo ----------------1
  32. echo "excu function func directly , and \$? get the return of functioin \"func\""
  33. func
  34. echo $?
  35. echo ----------------2
  36. echo "display the result of function func!"
  37. echo `func` ### is same as c=\$func\(\)

  38. #####    end of script    #####
  1. Result:
  2.     $./read-without-new-line.sh
  3.     Input your operate system:Linux
  4.     -----Out put ---
  5.     Your system is:
  6.     Linux
  7.     $c=func() # call of function func()
  8.     The value of $c is:
  9.     Function start .... OK-Linux Hehe, you knowe your operate
  10.     ----------------1
  11.     excu function func directly , and $? get the return of functioin "func"
  12.     Function start ....
  13.     OK-Linux
  14.     Hehe, you knowe your operate
  15.     99
  16.     ----------------2
  17.     display the result of function
  18.     Function start .... OK-Linux Hehe, you knowe your operate
  19.     
  20.     $ ./read-without-new-line.sh
  21.     Input your operate system:windows
  22.     -----Out put ---
  23.     Your system is:
  24.     Linux
  25.     $c=func()
  26.     The value of $c is:
  27.     Function start .... No-windows You don’t knowe your operate
  28.     ----------------1
  29.     excu function func directly , and $? get the return of functioin "func"
  30.     Function start ....
  31.     No-windows
  32.     You don‘t knowe your operate
  33.     100
  34.     ----------------2
  35.     display the result of function
  36.     Function start .... No-windows You don‘t knowe your operate 

脚本说明:
1、在同一行读取输入需要添加\c来指定。如脚本开始 #echo -e "Input your operate system:\c"
2、字符串匹配

2.1 # if [ $answer != `uname`]
2.2 # if [ "$answer" != "`uanme`" ]
2.3 # if [ $answer != $(uname) ]
3、函数返回值
3.1 $c=func(), 变量c得到的是func的所有输出,使用echo显示的时候没有换行。 使用 $c=func();echo $c的方式和 echo `func`的结果是一致的。
3.2 return 返回的值可以通过 $? 获取得到。
3.3 exit 调用和直接执行函数是不一样的。调用函数的时候,函数的输出会存到变量中。但是脚本继续执行。 如果是直接执行,脚本会终止。如果脚本中直接执行函数并且使用exit退出了脚本,那么在脚本外使用 $?  可以获得exit指定的数字。




阅读(2028) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~