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. day0203 XML 学习笔记

    day02, 03 1. xml语言和作用 2. xml语法详解 2.1 xml 语法声明 2.1.1 encoding 属性 2.1.2 standalone 属性 2.2 xml 元素(Eleme ...

  2. Android项目实战(四十五):Usb转串口通讯(CH34xUARTDriver)

    需求为:手机usb接口插入一个硬件,从硬件上获取数据 例如:手机usb插入硬件A,A通过蓝牙通讯获取设备a.b的数据,作为中转站(可以做些数据处理)将数据(设备a.b产生的)传给手机程序. 设备A也可 ...

  3. CSharpGL(48)用ShadowVolume画模型的影子

    CSharpGL(48)用ShadowVolume画模型的影子 在Per-Fragment Operations & Tests阶段,有一个步骤是模版测试(Stencil Test).依靠这一 ...

  4. Linux下查看进程打开的文件句柄数

    ---查看系统默认的最大文件句柄数,系统默认是1024 # ulimit -n ----查看当前进程打开了多少句柄数 # lsof -n|awk '{print $2}'|sort|uniq -c|s ...

  5. python脚本文件传参并通过token登录后爬取数据实例

    from bs4 import BeautifulSoup import requests import sys class Zabbix(object): def __init__(self, he ...

  6. 状态模式、职责链模式——省去if-else的繁琐结构

    小时候写日记都是这么写的:上午七点起床,八点之前洗脸刷牙吃早饭,十二点之前好好上课,中午一点,吃午饭,下午两点到六点,上课,下课,找请假,明天妈妈要带我去姥姥家,九点之前,看动画片,九点钟,收拾去姥姥 ...

  7. Linux Shell编程参考大全

    本文记录Linux Shell编程中常用基本知识,方便快速入门以及查询使用. 本文主要分为以下几个部分: 一.Shell中的变量 任何编程语言中,有关变量的定义,作用范围,赋值等都是最最基础的知识. ...

  8. mongo 存储过程

    摘要 本文主要介绍mongo存储过程,mongo 存储过程其实就是JS方法,然后通过eval 方法来执行,但是这个方法在3.0 depreate了,也就是在未来的版本,这个功能可能不提供了.从目前的j ...

  9. 解决linux删除文件后空间没有释放问题

    linux删除文件后沒有释放空间 今天发现一台服务器的home空间满了,于是要清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [ro ...

  10. antlr 4新特性总结及与antlr v3的不同

    antlr 4新特性总结及与antlr v3的不同 学习曲线低.antlr v4相对于v3,v4更注重于用更接近于自然语言的方式去解析语言.比如运算符优先级,排在最前面的规则优先级最高: 层次更清晰. ...