雨
分类: Python/Ruby
2011-04-07 14:20:04
- #!/bin/bash
- # read-without-new-line.sh
- # using \c to get input in the same line
- echo -e "Input your operate system:\c"
- read answer
- function func
- {
- echo "Function start ...."
- if [ $answer != `uname` ]
- # if [ $naswer != $(uanme) ] #### is same as above
- # if [ "$answer" != "`uname`" ] #### is same as above
- then
- echo No-$answer
- echo "You don't knowe your operate system!"
- # exit 5
- return 100
- else
- echo OK-$answer
- echo "Hehe, you knowe your operate system!"
- # exit 4
- return 99
- fi
- }
- echo -----Out put ---
- echo Your system is:
- echo $(uname)
- c=$(func)
- echo \$c=func\(\);
- echo The value of \$c is:
- echo $c
- echo ----------------1
- echo "excu function func directly , and \$? get the return of functioin \"func\""
- func
- echo $?
- echo ----------------2
- echo "display the result of function func!"
- echo `func` ### is same as c=\$func\(\)
- ##### end of script #####
- Result:
- $./read-without-new-line.sh
- Input your operate system:Linux
- -----Out put ---
- Your system is:
- Linux
- $c=func() # call of function func()
- The value of $c is:
- Function start .... OK-Linux Hehe, you knowe your operate
- ----------------1
- excu function func directly , and $? get the return of functioin "func"
- Function start ....
- OK-Linux
- Hehe, you knowe your operate
- 99
- ----------------2
- display the result of function
- Function start .... OK-Linux Hehe, you knowe your operate
- $ ./read-without-new-line.sh
- Input your operate system:windows
- -----Out put ---
- Your system is:
- Linux
- $c=func()
- The value of $c is:
- Function start .... No-windows You don’t knowe your operate
- ----------------1
- excu function func directly , and $? get the return of functioin "func"
- Function start ....
- No-windows
- You don‘t knowe your operate
- 100
- ----------------2
- display the result of function
- Function start .... No-windows You don‘t knowe your operate
2.1 # if [ $answer != `uname`]3、函数返回值
2.2 # if [ "$answer" != "`uanme`" ]
2.3 # if [ $answer != $(uname) ]
3.1 $c=func(), 变量c得到的是func的所有输出,使用echo显示的时候没有换行。 使用 $c=func();echo $c的方式和 echo `func`的结果是一致的。
3.2 return 返回的值可以通过 $? 获取得到。
3.3 exit 调用和直接执行函数是不一样的。调用函数的时候,函数的输出会存到变量中。但是脚本继续执行。 如果是直接执行,脚本会终止。如果脚本中直接执行函数并且使用exit退出了脚本,那么在脚本外使用 $? 可以获得exit指定的数字。