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. Java常用排序算法

    在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序. 一般来说外排序分为两个步骤:预处理和合并排序.首先,根据可用内存的大小,将外存上含有n个纪录的文件分成若干长 ...

  2. java中的构造,封装

    今天给大家讲一下面向对象中的构造,封装: 构造:构造方法有以下几个特点:1.方法名和类名一致.2.无返回类型.接下来的几种构造样式,直接上代码吧: //这是一个宠物类 有一个属性:名字(name) p ...

  3. 利用Python进行数据分析——Numpy基础:数组和矢量计算

    利用Python进行数据分析--Numpy基础:数组和矢量计算 ndarry,一个具有矢量运算和复杂广播能力快速节省空间的多维数组 对整组数据进行快速运算的标准数学函数,无需for-loop 用于读写 ...

  4. Bootstrap3 排版-引用

    在你的文档中引用其他来源的内容. 默认样式的引用 将任何 HTML 元素包裹在 <blockquote> 中即可表现为引用样式.对于直接引用,我们建议用 <p> 标签. Lor ...

  5. linux下内存的统计和内存泄露类问题的定位

    在产品的开发中,通过对当前系统消耗内存总量的统计,可以对产品所需内存总量进行精确的评估,从而选择合适的内存芯片与大小,降低产品的成本.在遇到内存泄露类问题时,经常会对此束手无策,本文通过对proc下进 ...

  6. 不可错过的Node.js框架

    前言 Node.js是由Ryan Dahl于2009年创建的.它是一个开源的跨平台运行时环境,用于开发服务器端和网络应用程序,它是基于Google Chrome V8 JavaScript引擎构建的. ...

  7. load balancer 配置参考

    https://wiki.ewu.edu/oit/Load_balancing_using_nginx

  8. Python 函数参数*expression 之后为什么只能跟关键字参数

    python 为何要设计这种? 正确: def f(a=2,b=2,c=3): return a+b+c print(f(*(1,1),c=1)) 错误: def f(a=2,b=2,c=3): re ...

  9. 使用Intent传递对象

    Intent 的用法相信你已经比较熟悉了,我们可以借助它来启动活动.发送广播.启动服务等.在进行上述操作的时候,我们还可以在Intent 中添加一些附加数据,以达到传值的效果,比如在FirstActi ...

  10. Linux Debugging (九) 一次生产环境下的“内存泄露”

    一个偶然的机会,发现一个进程使用了超过14G的内存.这个进程是一个RPC server,只是作为中转,绝对不应该使用这么多内存的.即使并发量太多,存在内存中的数据太多,那么在并发减少的情况下,这个内存 ...