case语句格式

# vi test.sh

:

echo "input : "

read num

echo "the input data is $num"



case $num in

1) echo "January";; 双分号结束

2) echo "Feburary";;

5) echo "may" 每个case可以有多条命令

echo "sdfd"

echo "sdf";; 但最后一条命令一定是双分号结束



*) echo "not correct input";; *)是其他值、default的意思



esac
# sh ./test.sh

input :

2

the input data is 2

Feburary



# sh ./test.sh

input :

ter

the input data is ter

not correct input

case 语句如果某个选项没有任何语句,也要加;; 否则会出下边错误

test: line 166: syntax error near unexpected
token `)'

test: line 166: `"system hostname config")'

匹配符[]是专门针对单字符的值,如果用[no],就是n和o之一

case $yn in

[no]) return 1;;

* ) echo "only accept Y,y,N,n,YES,yes,NO,no" >&2;;
[macg@mac-home ~]$ sh test.sh

enter y/n :

no

only accept Y,y,N,n,YES,yes,NO,no

改正

case $yn in

no) return 1;;

NO) return 1;;

* ) echo "only accept Y,y,N,n,YES,yes,NO,no" >&2;;

esac
[macg@mac-home ~]$ sh test.sh

enter y/n :

no

注意::

如果有多个单词可以用"|"隔开,如

case $yn in

start | begin ) return 0;;

end | over ) return 1;;

* ) return 3;;

if, case,匹配字符串最常见,但如何匹配一段很长的输出,一堆文字?最好方法,用“*”,如:*"command not found"*

[macg@machome ~]$ vi test.sh



var=$(ls -l $1)
$()取命令输出,$1是命令行参数

echo "output is $var"



case $var in

"-rw-rw-r--"*) echo "this is not a execute file";;

"-rwxrwxr-x"*) echo "this is a execute file";

注意*在双引号外边

esac

[macg@machome ~]$ sh test.sh 22.txt

output is -rw-rw-r-- 1 macg macg 15 Jun 9 19:00 22.txt

this is not a execute file



[macg@machome ~]$ chmod +x 22.txt

[macg@machome ~]$ sh test.sh 22.txt

output is -rwxrwxr-x 1 macg macg 15 Jun 9 19:00 22.txt

this is a execute file

这里需要注意的是:$(ls -l $1)
$()取命令输出

匹配是用两个**,因为整个var的内容是一行,要在两个之间匹配

例2.匹配file命令输出的一堆文字,以获知文件类型

用’ ’ 取输出,然后用CASE+*对输出做修饰处理.

var=`file $1` `
`和$( )作用相同,是取命令输出

echo "output is $var"



case $var in

"$1: ASCII text"*) echo "this is a text file";;

"$1: directory"*) echo "this is a directory";;

注意*在双引号外边

esac
[macg@machome ~]$ sh test.sh 22.txt

output is 22.txt: ASCII text

this is a text file



[macg@machome ~]$ sh test.sh test-dir

output is test-dir: directory

this is a directory

最典型的shell case命令匹配命令行,用于sys v启动脚本的start|stop|restart|status处理

case "$@" in

($@ 字符串数组:以"参数1" "参数2" ... 的字符串数组形式保存所有参数

对于单个参数的情况,$@就是一个字符串)



start)

echo -n "Starting
firewall..."

。。。

echo "OK!"

exit 0

;;

stop)

echo -n "Stopping
firewall..."

。。。

exit 0

;;

restart)

$0
stop $0即执行原始程序

$0
start

;;

status)

clear

echo ">------------------------------------------"


iptables -L

echo ">------------------------------------------"


iptables -t nat
-L POSTROUTING

exit 0

*)

echo "Usage: $0
{start|stop|restart|status}"

exit 1

esac

shell的case语句的更多相关文章

  1. (二)shell中case语句、程序传参、while

    2.2.6.1.case语句(1)shell中的case语句和C语言中的switch case语句作用一样,格式有差异(2)shell中的case语句天生没有break,也不需要break,和C语言中 ...

  2. shell的case语句简述(shell的流控制)

    shell流控制:http://www.cnblogs.com/yunjiaofeifei/archive/2012/06/12/2546208.html 1.if then else 语句 if t ...

  3. Shell 编程 case语句

    本篇主要写一些shell脚本case语句的使用. 字符判断 #!/bin/bash read -p "请输入一个字符:" char case $char in [a-z]|[A-Z ...

  4. linux bash shell中case语句的实例

    本文介绍下,在bash shell编程中,有关case语句的一个例子,学习下case语句的用法,有需要的朋友参考下. 本文转自:http://www.jbxue.com/article/13377.h ...

  5. Shell脚本case语句

    case语句格式 case 变量 in PAT1) 执行语句 ;; PAT2) 执行语句 ;; *) 默认执行语句 ;; esac 使用示例: 编写一个shell脚本,通过提示用户输入信息,输出cpu ...

  6. Linux Shell编程case语句

    http://blog.csdn.net/dreamtdp/article/details/8048720 case语句适用于需要进行多重分支的应用情况. case分支语句的格式如下: case $变 ...

  7. 【shell】case语句

    case只能判断一种条件关系,而if能判断多种条件关系 #!/bin/bash read -p "please input your choice (high/middle/low):&qu ...

  8. shell编程:case语句

  9. SHELL用法五(Case语句)

    1.SHELL编程Case语句案例实战 1)Case选择条件语句的格式: case $INPUT in Pattern1) 语句1 ;; Pattern2) 语句2 ;; esac 2)Case语句企 ...

随机推荐

  1. vuex存储和本地存储(localstorage、sessionstorage)的区别

    1.最重要的区别:vuex存储在内存,localstorage则以文件的方式存储在本地 2.应用场景:vuex用于组件之间的传值,localstorage则主要用于不同页面之间的传值. 3.永久性:当 ...

  2. UDP网络编程

    概念: UDP协议(用户数据报协议)是无连接,不可靠的,无序的.速度比较快, UDP协议以数据报作为数据传输的载体 进行数据传输时,首先将传输的数据定义成数据报(Datagram),在数据报中指明数据 ...

  3. Kinect 深度图像格式

    Kinect的深度图像有16bit,2byte,如图: 第15位:标志位,不用做深度计算 第14~3位:深度图像数据,即距离,以毫米为单位 第0~2位:深度图中人的ID(PlayerID) 深度图有两 ...

  4. Winform DevExpress控件库(二) 使用SplashScreenManager控件定制程序加载页面

    SplashScreenManager控件:主要作用是显示在进行耗时操作时的等待界面: 位于 工具箱 -> Navigation & Layout(导航栏与布局类控件) 目录下: 在工具 ...

  5. Bootstrap3 代码-内联代码

    通过 <code> 标签包裹内联样式的代码片段. For example, <section> should be wrapped as inline. For example ...

  6. 六星经典CSAPP-笔记(7)加载与链接(上)

    六星经典CSAPP-笔记(7)加载与链接 1.对象文件(Object File) 1.1 文件类型 对象文件有三种形式: 可重定位对象文件(Relocatable object file):包含二进制 ...

  7. LauncherModel.Callbacks接口

    public interface Callbacks { //如果Launcher在加载完成之前被强制暂停,那么需要通过这个回调方法通知 //launcher,在它再次显示的时候重新执行加载过程 pu ...

  8. linux中probe函数传递参数的寻找(下)

    点击打开链接 linux中probe函数传递参数的寻找(下) 通过追寻driver的脚步,我们有了努力的方向:只有找到spi_bus_type的填充device即可,下面该从device去打通,当两个 ...

  9. linux简单命常用令

    Linux常用命令总结 切换:cd tmp cd/tmp/yun cd 切换到host目录 cd .. 显示:ll Top显示系统情况 Netstat显示网络情况 Ifconfig显示网络配置 Mor ...

  10. [nginx] 对UA为空的请求返回403

    nginx blocking blank user agent . sometime apps' backgroud request always visit a url, and these req ...