Unix script 9 - case
来源:互联网 发布:安全网络征文大赛 编辑:程序博客网 时间:2024/05/21 11:29
case
statement saves going through a whole set of if .. then .. else
statements. Its syntax is really quitesimple:talk.sh
#!/bin/shecho "Please talk to me ..."while :do read INPUT_STRING case $INPUT_STRING in hello) echo "Hello yourself!" ;; bye) echo "See you again!" break ;; *) echo "Sorry, I don't understand" ;; esacdoneecho echo "That's all folks!"
Okay, so it's not the best conversationalist in the world; it's only anexample!
Try running it and check how it works...
$ ./talk.shPlease talk to me ...helloHello yourself!What do you think of politics?Sorry, I don't understandbyeSee you again!That's all folks!$The syntax is quite simple:
The
case
line itself is always of the same format, and itmeans that we are testing the value of the variableINPUT_STRING
.
The options we understand are then listed and followed by a right bracket,as hello)
and bye)
.
This means that ifINPUT_STRING
matches hello
then that sectionof code is executed, up to the double semicolon.
If INPUT_STRING
matches bye
then the goodbyemessage is printed and the loop exits. Note that if we wanted to exitthe script completely then we would use the commandexit
instead of break
.
The third option here, the *)
, is the default catch-allcondition; it is not required, but is often useful for debugging purposeseven if we think we know what values the test variable will have.
The whole case statement is ended with esac
(case backwards!)then weend the while loop with a done
.
That's about as complicated as case conditions get, but they can be avery useful and powerful tool. They are often used to parse theparameters passed to a shell script, amongst other uses.
- Unix script 9 - case
- TestComplete---Script Case
- Unix script - 7 Loop
- Unix script 17 - examples
- Unix Shell - Case statement
- 【Unix/Linux.Shell Script】Shell & Shell Script
- UNIX的批处理 Shell Script
- Unix script 5 - wildcards 星号
- Unix script - 6 escape chracter
- Unix script 15 - quick reference
- Unix script 16 - interactive shell
- 【Unix/Linux.Shell Script】函数
- Data Guard Switchover Unix shell script
- Unix scripting 3 - a first script
- Unix script 4 - variables part I
- Unix script 8 - Test if else
- Unix script 10 - variables II 输入变量
- Unix script 12 - extrnal programs 外部程序
- 动态内存分配
- linux消息队列进程通信
- Java 产生0-10000的随机数 并写入TXT文档中
- 索兰尼亚语
- poj2608
- Unix script 9 - case
- OpenCV合并图像
- 调度算法基础
- Linux wc命令详解
- AES算法分析与实现
- 八款开源 Android 游戏引擎
- Java环境搭配下载地址
- Oracle9i管理的V$视图介绍
- VMware虚拟机中Ubuntu 11.04系统开启ssh服务的方法